有什么方法可以调用包含 it() 和 运行 的 Mocha 函数吗?
Is there any way to call a Mocha function including it() and run whatever is inside `it`?
我正在尝试像下面这样简单地调用 Mocha 函数
this.logSomething = function() {
console.log('======== outside it ========')
it('something inside it', function logSomething(done){
console.log('+++++++ something inside it ++++++++')
done()
})
}
来自另一个 js 文件。
使用 mocha.run(logSomething())
后
======== outside it ========
出现但
+++++++ something inside it ++++++++
丢失。
我试过使用'describe',但结果是一样的。有没有绕过的解决方案?
仅供参考,我知道它可以 运行 通过将其导入为 Mocha 测试并使用 Mocha CLI,但是我想使用此方法重新 运行 我的测试套件中的失败函数,因此它可能是零个或多个具有不同名称的函数,并且它不像导入一定数量的 Mocha 测试那么简单。
此外,我已经尝试了 Mocha 现有的重试,但由于它不符合我们的测试,所以我没有使用它。
一个简单的例子
导出您的 logSomething
函数,将其导入另一个文件并执行它确实给了我您期望的行为:
lib.js
module.exports.logSomething = function() {
console.log('======== outside it ========')
it('something inside it', function logSomething(done){
console.log('+++++++ something inside it ++++++++')
done()
})
}
test.js
const {logSomething} = require('./lib');
logSomething();
logSomething();
输出
$ mocha test.js
======== outside it ========
======== outside it ========
+++++++ something inside it ++++++++
✓ something inside it
+++++++ something inside it ++++++++
✓ something inside it
2 passing (4ms)
包含多个文件和 classes
的示例
所以这是一个更复杂的示例:有一个 test.js
,它从两个单独的文件中导入两个 classes。每个 class 都有两种方法,依次使用 it()
.
运行 mocha 测试用例
test.js
const {TestSet1} = require('./lib1');
const {TestSet2} = require('./lib2');
new TestSet1().runTest1();
new TestSet1().runTest2();
new TestSet2().runTest1();
new TestSet2().runTest2();
lib1.js
class TestSet1 {
runTest1() {
it('should run TestSet1.Test1.It1', () => {
console.log('This is output from TestSet1.Test1.It1');
});
it('should run TestSet1.Test1.It2', () => {
console.log('This is output from TestSet1.Test1.It2');
});
}
runTest2() {
it('should run TestSet1.Test2.It1', () => {
console.log('This is output from TestSet1.Test2.It1');
});
it('should run TestSet1.Test2.It2', () => {
console.log('This is output from TestSet1.Test2.It2');
});
}
}
module.exports = {TestSet1};
lib2.js
class TestSet2 {
runTest1() {
it('should run TestSet2.Test1.It1', () => {
console.log('This is output from TestSet2.Test1.It1');
});
it('should run TestSet2.Test1.It2', () => {
console.log('This is output from TestSet2.Test1.It2');
});
}
runTest2() {
it('should run TestSet2.Test2.It1', () => {
console.log('This is output from TestSet2.Test2.It1');
});
it('should run TestSet2.Test2.It2', () => {
console.log('This is output from TestSet2.Test2.It2');
});
}
}
module.exports = {TestSet2};
输出
$ mocha test.js
This is output from TestSet1.Test1.It1
✓ should run TestSet1.Test1.It1
This is output from TestSet1.Test1.It2
✓ should run TestSet1.Test1.It2
This is output from TestSet1.Test2.It1
✓ should run TestSet1.Test2.It1
This is output from TestSet1.Test2.It2
✓ should run TestSet1.Test2.It2
This is output from TestSet2.Test1.It1
✓ should run TestSet2.Test1.It1
This is output from TestSet2.Test1.It2
✓ should run TestSet2.Test1.It2
This is output from TestSet2.Test2.It1
✓ should run TestSet2.Test2.It1
This is output from TestSet2.Test2.It2
✓ should run TestSet2.Test2.It2
8 passing (9ms)
我正在尝试像下面这样简单地调用 Mocha 函数
this.logSomething = function() {
console.log('======== outside it ========')
it('something inside it', function logSomething(done){
console.log('+++++++ something inside it ++++++++')
done()
})
}
来自另一个 js 文件。
使用 mocha.run(logSomething())
======== outside it ========
出现但
+++++++ something inside it ++++++++
丢失。
我试过使用'describe',但结果是一样的。有没有绕过的解决方案?
仅供参考,我知道它可以 运行 通过将其导入为 Mocha 测试并使用 Mocha CLI,但是我想使用此方法重新 运行 我的测试套件中的失败函数,因此它可能是零个或多个具有不同名称的函数,并且它不像导入一定数量的 Mocha 测试那么简单。
此外,我已经尝试了 Mocha 现有的重试,但由于它不符合我们的测试,所以我没有使用它。
一个简单的例子
导出您的 logSomething
函数,将其导入另一个文件并执行它确实给了我您期望的行为:
lib.js
module.exports.logSomething = function() {
console.log('======== outside it ========')
it('something inside it', function logSomething(done){
console.log('+++++++ something inside it ++++++++')
done()
})
}
test.js
const {logSomething} = require('./lib');
logSomething();
logSomething();
输出
$ mocha test.js
======== outside it ========
======== outside it ========
+++++++ something inside it ++++++++
✓ something inside it
+++++++ something inside it ++++++++
✓ something inside it
2 passing (4ms)
包含多个文件和 classes
的示例所以这是一个更复杂的示例:有一个 test.js
,它从两个单独的文件中导入两个 classes。每个 class 都有两种方法,依次使用 it()
.
test.js
const {TestSet1} = require('./lib1');
const {TestSet2} = require('./lib2');
new TestSet1().runTest1();
new TestSet1().runTest2();
new TestSet2().runTest1();
new TestSet2().runTest2();
lib1.js
class TestSet1 {
runTest1() {
it('should run TestSet1.Test1.It1', () => {
console.log('This is output from TestSet1.Test1.It1');
});
it('should run TestSet1.Test1.It2', () => {
console.log('This is output from TestSet1.Test1.It2');
});
}
runTest2() {
it('should run TestSet1.Test2.It1', () => {
console.log('This is output from TestSet1.Test2.It1');
});
it('should run TestSet1.Test2.It2', () => {
console.log('This is output from TestSet1.Test2.It2');
});
}
}
module.exports = {TestSet1};
lib2.js
class TestSet2 {
runTest1() {
it('should run TestSet2.Test1.It1', () => {
console.log('This is output from TestSet2.Test1.It1');
});
it('should run TestSet2.Test1.It2', () => {
console.log('This is output from TestSet2.Test1.It2');
});
}
runTest2() {
it('should run TestSet2.Test2.It1', () => {
console.log('This is output from TestSet2.Test2.It1');
});
it('should run TestSet2.Test2.It2', () => {
console.log('This is output from TestSet2.Test2.It2');
});
}
}
module.exports = {TestSet2};
输出
$ mocha test.js
This is output from TestSet1.Test1.It1
✓ should run TestSet1.Test1.It1
This is output from TestSet1.Test1.It2
✓ should run TestSet1.Test1.It2
This is output from TestSet1.Test2.It1
✓ should run TestSet1.Test2.It1
This is output from TestSet1.Test2.It2
✓ should run TestSet1.Test2.It2
This is output from TestSet2.Test1.It1
✓ should run TestSet2.Test1.It1
This is output from TestSet2.Test1.It2
✓ should run TestSet2.Test1.It2
This is output from TestSet2.Test2.It1
✓ should run TestSet2.Test2.It1
This is output from TestSet2.Test2.It2
✓ should run TestSet2.Test2.It2
8 passing (9ms)