gpt4 book ai didi

javascript - Mocha : call a test case synchronously

转载 作者:行者123 更新时间:2023-11-30 21:05:02 24 4
gpt4 key购买 nike

是否可以用Mocha同步调用测试用例?例如,我有以下代码:

context('Context of test suite', () => {
it('test case name', () => {
//call expect() a few times
})
})
console.log('foo')

我想运行它,但我想保证 foo 在测试用例执行并通过或失败之前不会被打印。 it 不返回 Promise,context 也不返回,所以我不能使用 then 来完成。我想要的是可能的吗?

这个测试用例存在于 after block 中。

最佳答案

您可以将 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 block 之外)以及 after 不是用于额外的测试,而是用于清理 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 移到套件之外,并将其提升为根级 Hook 。

关于javascript - Mocha : call a test case synchronously,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com