Skip to content

Instantly share code, notes, and snippets.

@chuckplantain
Created April 5, 2018 13:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckplantain/8359063a1c025b5ccfce52b4f5707368 to your computer and use it in GitHub Desktop.
Save chuckplantain/8359063a1c025b5ccfce52b4f5707368 to your computer and use it in GitHub Desktop.
Execution order of async Jest/Jasmine test code
/**
* 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