Mocha / Chair - 运行 在多个文件中测试
Mocha / Chair - run test in multiple files
我有一个通用测试,我想在多个测试文件中 运行,我做了一些研究,这是我发现将测试包含在一个文件中的建议解决方案:
目录结构:
|--test
|--common
|--common.js
|--common_functions.js
|--helpers.js
|--registration.js
common.js
var helpers = require("../../services/helpers");
var chai = require("chai");
var expect = require("chai").expect;
chai.should();
chai.use(require("chai-things"));
var testData = require("../../config/testData");
it('check if we are connected to local test db', function(done) {
helpers.checkTestDB(function(err, result) {
expect(err).to.equal(null);
result.should.equal('This is the test DB');
done();
});
});
common_functions.js
exports.importTest = function(name, path) {
describe(name, function () {
require(path);
});
}
helpers.js / registration.js
...
var common_functions = require('./common_functions');
...
describe("Common Tests Import", function(){
common_functions.importTest("checkDb",'./common/common');
});
问题是测试只在两个文件之一上 运行s,如果我把它留在两个文件中 运行s 在 helpers 上,如果我注释掉 helpers,注册一个运行s,有没有办法在每一个中 运行 它?
原因是我在每个文件中设置 env 变量以使用测试数据库,但是有很多事情要做,以防它以某种方式改变我希望它成为 运行分别在每个文件上。
您需要在 common.js
中执行类似于您在 common_functions.js
中执行的操作:export 调用 it
的函数,而不是而不是 it
像现在这样坐在顶层。所以将 common.js
修改成这样:
var helpers = require("../../services/helpers");
var chai = require("chai");
var expect = require("chai").expect;
chai.should();
chai.use(require("chai-things"));
var testData = require("../../config/testData");
module.exports = function () {
it('check if we are connected to local test db', function(done) {
helpers.checkTestDB(function(err, result) {
expect(err).to.equal(null);
result.should.equal('This is the test DB');
done();
});
});
};
然后你导入模块后调用这个函数。所以把 common_functions.js
改成这样:
exports.importTest = function(name, path) {
describe(name, function () {
// We call the function exported by the module.
require(path)();
});
}
否则问题在于,因为CommonJS模块是单例的,那么common.js
中的it
调用会被执行一次,并且只会执行一次,当 Node 读取文件并在内存中创建模块时。随后的 require('./common/common')
次调用不会再次执行该模块的代码。
我有一个通用测试,我想在多个测试文件中 运行,我做了一些研究,这是我发现将测试包含在一个文件中的建议解决方案:
目录结构:
|--test
|--common
|--common.js
|--common_functions.js
|--helpers.js
|--registration.js
common.js
var helpers = require("../../services/helpers");
var chai = require("chai");
var expect = require("chai").expect;
chai.should();
chai.use(require("chai-things"));
var testData = require("../../config/testData");
it('check if we are connected to local test db', function(done) {
helpers.checkTestDB(function(err, result) {
expect(err).to.equal(null);
result.should.equal('This is the test DB');
done();
});
});
common_functions.js
exports.importTest = function(name, path) {
describe(name, function () {
require(path);
});
}
helpers.js / registration.js
...
var common_functions = require('./common_functions');
...
describe("Common Tests Import", function(){
common_functions.importTest("checkDb",'./common/common');
});
问题是测试只在两个文件之一上 运行s,如果我把它留在两个文件中 运行s 在 helpers 上,如果我注释掉 helpers,注册一个运行s,有没有办法在每一个中 运行 它?
原因是我在每个文件中设置 env 变量以使用测试数据库,但是有很多事情要做,以防它以某种方式改变我希望它成为 运行分别在每个文件上。
您需要在 common.js
中执行类似于您在 common_functions.js
中执行的操作:export 调用 it
的函数,而不是而不是 it
像现在这样坐在顶层。所以将 common.js
修改成这样:
var helpers = require("../../services/helpers");
var chai = require("chai");
var expect = require("chai").expect;
chai.should();
chai.use(require("chai-things"));
var testData = require("../../config/testData");
module.exports = function () {
it('check if we are connected to local test db', function(done) {
helpers.checkTestDB(function(err, result) {
expect(err).to.equal(null);
result.should.equal('This is the test DB');
done();
});
});
};
然后你导入模块后调用这个函数。所以把 common_functions.js
改成这样:
exports.importTest = function(name, path) {
describe(name, function () {
// We call the function exported by the module.
require(path)();
});
}
否则问题在于,因为CommonJS模块是单例的,那么common.js
中的it
调用会被执行一次,并且只会执行一次,当 Node 读取文件并在内存中创建模块时。随后的 require('./common/common')
次调用不会再次执行该模块的代码。