Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw [jscoverage] unknow ENV! error when define variable window #24

Open
hotoo opened this issue Sep 18, 2014 · 3 comments
Open

throw [jscoverage] unknow ENV! error when define variable window #24

hotoo opened this issue Sep 18, 2014 · 3 comments

Comments

@hotoo
Copy link

hotoo commented Sep 18, 2014

https://github.com/fishbar/jscoverage/blob/master/lib/jscoverage.js#L36

    if (typeof global === 'object') {
      BASE = global;
    } else if (typeof window === 'object') {
      BASE = window;
    } else {
      throw new Error('[jscoverage] unknow ENV!');
    }

when my code has variable window,

var window = this; // this ref to window.
console.log(typeof window);

will console log object run in node, build as cmd module and run in web, or run in phantom.
but throw error [jscoverage] unknow ENV! here, why?

ref

@fishbar
Copy link
Owner

fishbar commented Oct 6, 2014

没看懂你的意思。。。, 运行环境能描述一下么?

@hotoo
Copy link
Author

hotoo commented Oct 6, 2014

背景

  1. spm 使用了 jscoverage 来生成测试覆盖率
  2. detector 模块中书写了 var window = this 这样的代码,在浏览器中运行时指向 window
  3. 使用 spm test 运行测试最终会调用 jscoverage 模块来生成覆盖率
  4. 但是测试 detector 模块时,jscoverage 抛出 [jscoverage] unknow ENV! 异常。

分析

看了 jscoverage 的代码:

    if (typeof global === 'object') {
      BASE = global;
    } else if (typeof window === 'object') {
      BASE = window;
    } else {
      throw new Error('[jscoverage] unknow ENV!');
    }

这里抛出了异常,但是实际上,即使 detector 中重定义 window 变量,typeof window 还是等于 "object" 的,不知道为什么 jscoverage 这里会抛出这个异常。

@fishbar
Copy link
Owner

fishbar commented Oct 7, 2014

"detector 模块中书写了 var window = this 这样的代码"
这句有可能会影响到,因为jscoverage的这段inject在代码的最前面,
当你的代码中出现 var window = this 作为一个全局的声明,
会将window重新define(如果window已经存在的话) (部分js引擎的解析貌似是这个样子)
当执行到这个statement时才赋值为 this, 所以会出现window为undefined的情况,也就命中了jscoverage的判断。

我去看看 spm test 做了什么

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants