我试图将一些以编程方式生成的测试与node.js中的Mocha测试框架集成。
var Promise = require('promise');
var resolved = Promise.resolve(true);
suite("Synchronously defined suite", function() {
test("Synchronously defined test", function() {
return resolved;
});
});
defer(function() {
suite("Asynch DEFINED test", function() {
suiteSetup(function() {
console.log("Asynch setupSuite running...");
});
test("Asynch Test", function() {
console.log("Async test running...");
return resolved;
});
});
});
function defer(fn) {
console.log("Calling defer...");
setTimeout(fn, 100);
}
当我执行此操作时,我得到:
$ mocha --ui tdd nested-test.js
Calling defer...
Synchronously defined suite
✓ Synchronously defined test
1 passing (4ms)
似乎Mocha要求所有"套件"函数在模块加载时同步执行 - 延迟调用似乎(默默地)被忽略(或程序在调用之前退出)。
答案 0 :(得分:1)
这是我设法提出的。它不会在mocha下运行,而是作为一个独立的节点程序运行:
var Promise = require('promise');
var Mocha = require('mocha');
var resolved = Promise.resolve(true);
var rejected = Promise.reject(new Error("Failing test."));
defer(function() {
console.log("Programmatic test suite creation.");
var suite = new Mocha.Suite("Programatic Suite");
var runner = new Mocha.Runner(suite);
var reporter = new Mocha.reporters.Spec(runner);
suite.addTest(new Mocha.Test("My test", function() {
return resolved;
}));
suite.addTest(new Mocha.Test("My (failing) test", function() {
return rejected;
}));
runner.run();
});
function defer(fn) {
setTimeout(fn, 100);
}
运行时输出:
$ node deferred-test.js
Programmatic test suite creation.
Programatic Suite
✓ My test
1) My (failing) test
1 passing (7ms)
1 failing
1) Programatic Suite My (failing) test:
Error: Failing test.
at Object.<anonymous> (deferred-test.js:5:31)
at node.js:935:3
我希望能够在我的应用程序中的所有(静态定义的)mocha运行测试套件中包含此测试。