javascript 摩卡全球范围问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20737252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mocha Global Scoping Issues
提问by Jeff Allen
I'm having a big problem with my mocha tests around a global object I'm using. I'm able to produce the following MRE which doesn't give the exact same error, but exemplifies the problematic (buggy?) behavior. Any insight would be much appreciated.
我在围绕我正在使用的全局对象进行摩卡测试时遇到了一个大问题。我能够生成以下 MRE,它不会给出完全相同的错误,但举例说明了有问题的(有问题的?)行为。任何见解将不胜感激。
I have the following main.js
file in /lib
:
我有以下main.js
文件/lib
:
exports.exec = function(){
console.log(test);
}
Then the following in /test/test.js
:
然后在以下内容/test/test.js
:
var should = require('should');
var main = require('../lib/main');
global.test = {something: 1};
describe('normal test', function(){
beforeEach(function(){
global.test = {another: 2};
}),
afterEach(function(){
delete global.test;
});
it ('might work with global', function(){
main.exec();
})
});
Finally, this is test/test2.js
:
最后,这是test/test2.js
:
var should = require('should');
var main = require('../lib/main');
global.test = {third: 3};
describe('some test', function(){
it ('messes up global', function(){
main.exec();
})
});
I expect that the first test would output {another:2}
and the second would print {third: 3}
. Indeed, this is the behavior I get when I run each test independently. e.g.
我希望第一个测试会输出{another:2}
,第二个会打印{third: 3}
。事实上,这是我独立运行每个测试时得到的行为。例如
jeff@ubuntu:~/workspace/mocha-test$ mocha test/test2.js
{ third: 3 }
?
1 passing (6ms)
However, when running both test with npm packages should
and mocha
(1.16.1), I get the following output:
但是,当使用 npm 包should
和mocha
(1.16.1)运行测试时,我得到以下输出:
jeff@ubuntu:~/workspace/mocha-test$ mocha
{ another: 2 }
??
1 passing (6ms)
1 failing
1) some test messes up global:
ReferenceError: test is not defined
at Object.exports.exec (/home/jeff/workspace/mocha-test/lib/main.js:3:15)
at Context.<anonymous> (/home/jeff/workspace/mocha-test/test/test2.js:8:10)
at Test.Runnable.run (/usr/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/lib/node_modules/mocha/lib/runner.js:355:10)
at /usr/lib/node_modules/mocha/lib/runner.js:401:12
at next (/usr/lib/node_modules/mocha/lib/runner.js:281:14)
at /usr/lib/node_modules/mocha/lib/runner.js:290:7
at next (/usr/lib/node_modules/mocha/lib/runner.js:234:23)
at Object._onImmediate (/usr/lib/node_modules/mocha/lib/runner.js:258:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
回答by Jeff Allen
test2.js
should be structured like this:
test2.js
应该是这样的结构:
describe('some test', function(){
before(function (){
global.test = {third: 3};
});
it ('messes up global', function(){
main.exec();
})
});
travisjeffery on the GitHub issue mentioned in the comment explains:
评论中提到的 GitHub 问题上的 travisjeffery 解释说:
mocha loads the files and then runs the suites, to reliably setup your tests the setup should be within the suite.
mocha 加载文件然后运行套件,为了可靠地设置你的测试,设置应该在套件内。
As @SB points out, this may not be amenable to sharing things like global variables across tests.
正如@SB 指出的那样,这可能不适合跨测试共享全局变量之类的东西。