我的mocha
规范的定义方式是描述具有一个全局数组(声明为空),该数组在我的第一个it
中进行访问和修改。第二个测试是itParam
(请参阅mocha-param)测试,该测试尝试访问它,但发现它为空,因此itParam
测试根本不运行。
示例(it
可以访问修改后的全局值):
describe('some test', function () {
var arr = [];
it('should pass a value', function (done) {
arr.push(5);
done();
});
it('and then double it', function (done) {
console.log(arr); // [5]
done();
});
});
示例(itParam
无法访问已修改的全局值):
let itParam = require('mocha-param');
describe('some test', function () {
var arr = [];
it('should pass a value', function (done) {
arr.push(5);
done();
});
itParam('and then double it', arr, function (done, v) {
console.log(v); // []
done();
});
});
PS:我了解,不建议在一种测试中修改值然后在另一项测试中使用它的做法,但这对我来说是不可避免的。