Reputation: 2318
Take the below code:
describe('foobar', function () {
it('foo', async () => {
// ############ API Call ############
let foo = await chaiHttpUtil.postRequest(yadda, yadda yadda);
// ############ Verify results ############
expect(foo).to.have.status(200);
));
it('bar', async () => {
// ############ API Call ############
let bar = await chaiHttpUtil.postRequest(yadda, yadda yadda);
// ############ Verify results ############
expect(bar).to.have.status(200);
));
));
When the above is run, I see that the first "it" (i.e. foo) is kicked off then, instead of waiting for it to finish, it then kicks off the second "it" (i.e. bar).
How can I get bar to wait until foo is completed? Can I? I understand the nature of async
is so that it is just waiting to finish, but can I put something in the bar to get it to wait for the first to complete?
Update:
I made each test independent from state and each other. Works great now. Thanks for the guidance!
Upvotes: 0
Views: 797
Reputation: 1348
As mentioned by, @k0pernikus, "each it should be independent of one another".
Some possible solutions:
You could combine the tests into one it
function since they are dependent on one another.
State changes should not be propagated to other tests and the state should be restored for each test. Using beforeEach
/afterEach
hooks can help you achieve this. See: https://mochajs.org/#using-async-await
You could utilize this delayed root-setup (https://mochajs.org/#delayed-root-suite), but I don't suspect this is what you're looking for.
Another thing to note is that:
Passing arrow functions (aka “lambdas”) to Mocha is discouraged.
You should be using
it('bar', async function () { //... });
instead of
it('bar', async () => { //... });
See: https://mochajs.org/#arrow-functions
Upvotes: 1