Skip to content

Commit

Permalink
fix: run test cleanup hooks in reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 9, 2023
1 parent 24f5862 commit 7463b4a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export class TestRunner {
this.#hooks.clear('cleanup')
try {
debug('running "%s" test cleanup functions', this.#test.title)
await cleanupRunner.run(this.#hasError, this.#test)
await cleanupRunner.runReverse(this.#hasError, this.#test)
} catch (error) {
debug('test cleanup functions failed, test: %s, error: %O', this.#test.title, error)
this.#hasError = true
Expand Down
36 changes: 36 additions & 0 deletions test/test/execute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,42 @@ test.describe('execute | hooks', () => {
assert.lengthOf(event!.errors, 0)
assert.deepEqual(stack, ['executed', 'test:cleanup', 'executed', 'test:cleanup'])
})

test('run test cleanup hooks in reverse order', async () => {
const stack: string[] = []
const emitter = new Emitter()
const refiner = new Refiner({})

const testInstance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner)
testInstance.with(['foo', 'bar'])

testInstance.run(async () => {
testInstance.cleanup(async (hasError, t) => {
assert.isFalse(hasError)
assert.deepEqual(t, testInstance)
stack.push('test:cleanup')
})
testInstance.cleanup(async (hasError, t) => {
assert.isFalse(hasError)
assert.deepEqual(t, testInstance)
stack.push('test:cleanup 1')
})

stack.push('executed')
})

const [, event] = await Promise.all([testInstance.exec(), pEvent(emitter, 'test:end', 8000)])
assert.isFalse(event!.hasError)
assert.lengthOf(event!.errors, 0)
assert.deepEqual(stack, [
'executed',
'test:cleanup 1',
'test:cleanup',
'executed',
'test:cleanup 1',
'test:cleanup',
])
})
})

test.describe('execute | executing', () => {
Expand Down

0 comments on commit 7463b4a

Please sign in to comment.