Reputation: 13283
I've been baffled for the last few days by how Jest deals with beforeAll
inside describe
blocks. I want each describe
's beforeAll
to only execute when it's time to run the tests inside that describe
.
I've written this test file (test test file?):
async function wait1sec() {
return new Promise(resolve => setTimeout(resolve, 1000));
}
async function logWait(i, isDescribe = false) {
console.log(`before wait inside ${ isDescribe ? 'describe' : 'it' } #` + i);
await wait1sec();
console.log(`after wait inside ${ isDescribe ? 'describe' : 'it' } #` + i);
expect(true).toEqual(true);
}
function logSync(i, isBefore = false) {
console.log(`synchronous function ${ isBefore ? 'before' : 'it' } #${ i }`);
}
describe('async behavior', () => {
describe('block 1', () => {
const i = 1;
beforeAll(() => logSync(i, true));
it('sync test in block 1', () => logSync(i));
});
describe('block 2', () => {
const i = 2;
beforeAll(() => logSync(i, true));
it('sync test in block 1', () => logSync(i));
});
});
You can see the remnants of the fact that I was testing this in the context of async behavior but it turns out that it's confusing even without async.
What I hope get is:
synchronous function before #1
synchronous function it #1
synchronous function before #2
synchronous function it #2
What I actually get is
synchronous function before #1
synchronous function before #2
synchronous function it #1
synchronous function it #2
What's even worse is that if I skip the second block (with xdescribe
), the second beforeAll
still runs!
So I guess it's not that confusing, I guess it's just pretty clear how it works, it's just not the way I want it to work.
Is there any way, short of simply putting my "beforeAll" code inside my first test of the block, to get the result I want?
Note: For a bit of context, I'm using these tests to interact with an external REST API to test how I've set up a workflow. In other words, I'm not testing my own code, and all the interactions with the server are real and result in real CRUD transactions. They're not significant, and they're easy to clean up, so it's a safe and controlled thing to be doing. And my tests build on each other, from one describe
to the next (i.e., the page I'm working with is created once, before the very first test, and modified with the REST API throughout the remainder) which means that a beforeAll
in describe
#2 running before the tests in describe
#1 can mess up the whole thing. (Especially if I'm trying to skip blocks with xdescribe
!)
Upvotes: 4
Views: 2765
Reputation: 2745
You can use -i
flag for run your test sequentially;
Also You can wrap your describe blocks into IIFE (or add function for that):
describe('async behavior', () => {
(() =>
describe('block 1', () => {
...
}))();
(() =>
describe('block 2', () => {
...
}))();
});
and it will work as You expect, but I'm not sure is it a good practice. Maybe this way will create some other logical errors for some other async behaviour;
Upvotes: 0