-
Notifications
You must be signed in to change notification settings - Fork 1
/
after-each.ts
50 lines (46 loc) · 2.04 KB
/
after-each.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import "reflect-metadata"
import { TestOptionsInput, parseTestOptions, isTestOptionsInput } from "./options"
import { configuration } from "./configure"
export type AfterEachFunction = (instance: any) => void
export function getAfterEachs(target: Object): AfterEachFunction[] {
const afterEachs = Reflect.getMetadata("test-decorators:afterEachs", target)
if (typeof afterEachs !== "undefined") { return afterEachs }
const newAfterEachContainer: AfterEachFunction[] = []
Reflect.defineMetadata("test-decorators:afterEachs", newAfterEachContainer, target)
return newAfterEachContainer
}
export type AfterEachDecorator =
(target: Object, property: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor
export function afterEach<Params>(
target: Object,
property: string | symbol,
descriptor: PropertyDescriptor,
): PropertyDescriptor
/**
* Decorate a method with `@afterEach` to have it run after each it-call in the suite.
*
* @return The decorated method.
*/
export function afterEach<Params>(
arg1: Object,
arg2?: string | symbol,
arg3?: PropertyDescriptor,
) {
// Load the `afterEach` function from the configuration which is stored singleton.
// It might have been configured to overwrite the mocha ones.
const { afterEach: afterEachFn } = configuration
if (typeof afterEachFn !== "function") {
throw new Error("'afterEach' not found. Did you call 'configure'?")
}
const decorator: AfterEachDecorator = (target, property, descriptor) => {
const afterEachs = getAfterEachs(target.constructor)
afterEachs.push(instance => {
// This prevents the super-class from executing the afterEach-functions of the extending classes.
if (!(instance instanceof target.constructor)) { return }
afterEachFn(async (...args: any[]) => await descriptor.value.apply(instance, args))
})
return descriptor
}
// Function was used as a decorator without a configuration (`@afterEach`).
return decorator(arg1, arg2, arg3)
}