-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
index.ts
75 lines (67 loc) · 1.8 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
PromiseWithSubtest,
TapPlugin,
TestBase,
Worker,
WorkerOpts,
} from '@tapjs/core'
import { isMainThread, workerData } from 'node:worker_threads'
export type PromiseWithWorker = PromiseWithSubtest<Worker>
export class WorkerPlugin {
#t: TestBase
#workerData?: any
/**
* True if in the main thread. False when running in a worker thread
* spawned by {@link @tapjs/worker!WorkerPlugin#worker | t.worker}
*
* @group Test Reflection
*/
isMainThread: boolean
constructor(t: TestBase) {
this.#t = t
this.isMainThread = isMainThread
// covered by tests, but V8 coverage doesn't extend to worker threads
/* c8 ignore start */
if (!isMainThread) this.#workerData = workerData
/* c8 ignore stop */
}
/**
* In the worker thread, the worker data that was provided to the
* {@link @tapjs/worker!WorkerPlugin#worker | t.worker} method.
*
* In the main thread, this field is `undefined`.
*/
get workerData() {
return this.#workerData
}
/**
* Start a Node Worker thread and parse its standard output as a child test
*
* @group Subtest Methods
*/
worker(filename: string): PromiseWithWorker
worker(filename: string, name?: string): PromiseWithWorker
worker(
filename: string,
options: WorkerOpts,
name?: string
): PromiseWithWorker
worker(
filename: string,
options?: WorkerOpts | string,
name?: string
): PromiseWithWorker {
if (typeof options === 'string') {
name = options
options = {}
}
options = options || {}
if (options.name === undefined && name !== undefined) {
options.name = name
}
options.filename = filename
return this.#t.sub(Worker, options, this.#t.t.worker)
}
}
export const plugin: TapPlugin<WorkerPlugin> = (t: TestBase) =>
new WorkerPlugin(t)