作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以用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/
我是一名优秀的程序员,十分优秀!