Is it possible to call a test case synchronously with Mocha? For example, I have the code below:
context('Context of test suite', () => {
it('test case name', () => {
//call expect() a few times
})
})
console.log('foo')
I want to run this, but I want to guarantee that foo
will not be printed until AFTER the test case has executed and either passed or failed. it
does not return a Promise, nor does context
, so I can't do it using then
. Is what I want possible?
This test case exists inside the after
block.
You can move that console.log
into a nested 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');
});
})
});
});
Although I have a hard time understanding why you'd use a setup like this. One issue is that you can't use this setup inside a root-level after
(one that's outside of any describe
block), and also, after
isn't meant for additional tests, it's meant to clean up after tests.
I would probably use something like this:
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');
});
})
Ie just place the suite that has to run last, well, last. You can move that last after
outside of the suite and promote it to a root-level hook, if you like.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.