Created
April 5, 2018 13:59
-
-
Save chuckplantain/8359063a1c025b5ccfce52b4f5707368 to your computer and use it in GitHub Desktop.
Execution order of async Jest/Jasmine test code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Demonstrate execution order of code in Jest/Jasmine | |
*/ | |
function resolveAfter2Seconds(message) { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(message) | |
}, Math.ceil(Math.random() * 5000)) | |
}) | |
} | |
async function promiseLog(message) { | |
// console.log(`${message} -> begining`) | |
const result = await resolveAfter2Seconds(message) | |
console.log(result) | |
} | |
describe('First test', () => { | |
beforeAll(async (done) => { | |
await promiseLog('Before all') | |
done() | |
}) | |
afterAll(async (done) => { | |
await promiseLog('After all') | |
done() | |
}) | |
beforeEach(async (done) => { | |
await promiseLog('Before each') | |
done() | |
}) | |
afterEach(async (done) => { | |
await promiseLog('After each') | |
done() | |
}) | |
describe('Inner', () => { | |
beforeAll(async (done) => { | |
await promiseLog('Before all inner') | |
done() | |
}) | |
afterAll(async (done) => { | |
await promiseLog('After all inner') | |
done() | |
}) | |
beforeEach(async (done) => { | |
await promiseLog('Before each inner') | |
done() | |
}) | |
afterEach(async (done) => { | |
await promiseLog('After each inner') | |
done() | |
}) | |
it('runs a test', async () => { | |
await promiseLog('>>> Running first test') | |
}) | |
it('runs another test', async () => { | |
await promiseLog('>>> Running second test') | |
}) | |
}) | |
it('runs this test too', async () => { | |
await promiseLog('>>> Running third test') | |
}) | |
}) | |
describe('Second test', () => { | |
it.skip("doesn't run this test", async () => { | |
await promiseLog('Nope') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment