feat: basic beforeEach, afterEach support (#28)
This commit is contained in:
parent
e216c2d04e
commit
85af7dd4cb
4 changed files with 71 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
export { default as test, it } from './testComponents/test'
|
||||
export { default as describe } from './testComponents/describe'
|
||||
export { default as expect } from './testComponents/expect'
|
||||
export { beforeEach, afterEach } from './testComponents/hooks'
|
||||
|
|
9
src/testComponents/hooks.ts
Normal file
9
src/testComponents/hooks.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { getTestContext } from '../testContext'
|
||||
|
||||
export function beforeEach(func: () => void) {
|
||||
getTestContext().addBeforeEach(func)
|
||||
}
|
||||
|
||||
export function afterEach(func: () => void) {
|
||||
getTestContext().addAfterEach(func)
|
||||
}
|
|
@ -21,6 +21,8 @@ export function setContext(context: TestContext | null) {
|
|||
export class TestContext {
|
||||
children: Map<string, TestContext>
|
||||
tests: Map<TestCaseLabel, TestCaseFunction>
|
||||
beforeEach?: () => void
|
||||
afterEach?: () => void
|
||||
parentContext?: TestContext | null
|
||||
|
||||
constructor(parentContext: TestContext | null = null) {
|
||||
|
@ -39,11 +41,38 @@ export class TestContext {
|
|||
return childContext
|
||||
}
|
||||
|
||||
addBeforeEach(func: () => void) {
|
||||
if (this.beforeEach) throw new Error('beforeEach is already defined on context.')
|
||||
this.beforeEach = func
|
||||
}
|
||||
|
||||
addAfterEach(func: () => void) {
|
||||
if (this.afterEach) throw new Error('afterEach is already defined on context.')
|
||||
this.afterEach = func
|
||||
}
|
||||
|
||||
get allBeforeEach(): Array<() => void> {
|
||||
if (!this.beforeEach) return this.parentContext?.allBeforeEach ?? [() => {}]
|
||||
|
||||
const parentBeforeEach = !this.parentContext ? [] : this.parentContext.allBeforeEach
|
||||
|
||||
return [...parentBeforeEach, this.beforeEach]
|
||||
}
|
||||
|
||||
get allAfterEach(): Array<() => void> {
|
||||
if (!this.afterEach) return this.parentContext?.allAfterEach ?? [() => {}]
|
||||
|
||||
const parentAfterEach = !this.parentContext ? [] : this.parentContext.allAfterEach
|
||||
|
||||
return [...parentAfterEach, this.afterEach]
|
||||
}
|
||||
runTest(label: TestCaseLabel, test: TestCaseFunction) {
|
||||
performance.mark(`test-${label}:start`)
|
||||
let hasFailed = false
|
||||
try {
|
||||
this.allBeforeEach.forEach((func) => func())
|
||||
test()
|
||||
this.allAfterEach.forEach((func) => func())
|
||||
} catch (e) {
|
||||
hasFailed = true
|
||||
logger.logError(String(e))
|
||||
|
|
32
tests/hooks.test.ts
Normal file
32
tests/hooks.test.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
import assert from 'assert'
|
||||
|
||||
import { test, describe, beforeEach, afterEach, expect } from 'works-on-my-machine'
|
||||
|
||||
describe('Test group lifecycle hooks', () => {
|
||||
let outer: boolean = false
|
||||
|
||||
beforeEach(() => {
|
||||
outer = true
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
outer = false
|
||||
})
|
||||
|
||||
describe('beforeEach', () => {
|
||||
let inner: boolean = false
|
||||
|
||||
beforeEach(() => {
|
||||
inner = true
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
inner = false
|
||||
})
|
||||
|
||||
test('all beforeEach side-effects run before each test runs', () => {
|
||||
expect(inner).toBe(true)
|
||||
expect(outer).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
Reference in a new issue