-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.ts
More file actions
67 lines (57 loc) · 1.7 KB
/
Copy pathtest.ts
File metadata and controls
67 lines (57 loc) · 1.7 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Result from './result'
import Runnable, { isRunnable, RunnableOptions, RunnableTypes } from './runnable'
import { RunOptions } from './runner'
import Suite from './suite'
export type TestFn = () => (void | Promise<any>)
/**
* @description Checks if the passed `Runnable` value is a `Test` instance.
*/
export const isTest = (v: unknown): v is Test => {
if (!isRunnable(v)) { return false }
return v.type === RunnableTypes.Test
}
export default class Test extends Runnable {
public fn: TestFn
public type: RunnableTypes.Test = RunnableTypes.Test
/* istanbul ignore next */
constructor(description: string, fn: TestFn, options: Partial<RunnableOptions> = {}, parent: Suite | null) {
super(description, options, parent)
this.fn = fn
this.parent = parent
}
/**
* @description Run a `Test` instance.
*/
public async run(options?: Partial<RunOptions>): Promise<Result> {
if (this.options.skip || this.options.todo) {
return this.doSkip(this.options.todo)
}
this.doStart()
if (options && options.timeout) {
let timer: NodeJS.Timeout
const wait = (ms: number) => new Promise(resolve => {
timer = setTimeout(resolve, ms)
})
const test = Promise.race([
wait(options.timeout).then(() => {
clearTimeout(timer)
throw new Error(`${this.getFullDescription()} has timed out: ${options.timeout}ms`)
}),
this.fn()
])
try {
await test
} catch (error) {
return this.doFail(error)
}
return this.doPass()
} else {
try {
await this.fn()
} catch (error) {
return this.doFail(error)
}
return this.doPass()
}
}
}