摩卡:同步调用测试用例
问题描述:
是否可以与摩卡同步调用测试用例?例如,我有下面的代码:摩卡:同步调用测试用例
context('Context of test suite',() => {
it('test case name',() => {
//call expect() a few times
})
})
console.log('foo')
我要运行它,但我要保证foo
将不被打印后,才测试用例已经执行,要么通过或失败。 it
不会返回Promise,也不会返回context
,所以我无法使用then
。我想要什么?
该测试用例存在于after
块内。
答
您可以移动console.log
到嵌套after
:
describe('all my tests',() => {
it('#1', done => setTimeout(done, 500))
it('#2', done => setTimeout(done, 500))
it('#3', done => setTimeout(done, 500))
after(() => {
context('Context of test suite',() => {
it('test case name',() => {})
after(() => {
console.log('foo');
});
})
});
});
虽然我很难理解为什么要使用这样的设置。一个问题是,您不能在根级after
(其中一个位于任何describe
块之外)中使用此设置,并且after
不适用于其他测试,它意味着在测试之后清除。
我可能会使用这样的事:
describe('all my tests',() => {
it('#1', done => setTimeout(done, 500))
it('#2', done => setTimeout(done, 500))
it('#3', done => setTimeout(done, 500))
});
describe('Context of test suite',() => {
it('test case name',() => {})
after(() => {
console.log('foo');
});
})
即只需放置最后运行的套件即可。如果您愿意,您可以将最后的after
移到套件外部,并将其提升到根级挂钩。
你究竟想达到什么目的?我相信你知道[XY问题](https://meta.stackexchange.com/a/66378) – Sidney
我不明白这个问题。我试图让一个测试用例同步执行。 – ewok
测试会同步运行,除非您在内部执行异步操作。如果你只调用'expect()'几次就不会有问题。 –