-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
worker.ts
176 lines (165 loc) · 5.14 KB
/
worker.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { formatLocation } from '../../formatter/helpers'
import {
ICoordinatorReport,
IWorkerCommand,
IWorkerCommandInitialize,
IWorkerCommandRun,
} from './command_types'
import { EventEmitter } from 'events'
import StackTraceFilter from '../../stack_trace_filter'
import supportCodeLibraryBuilder from '../../support_code_library_builder'
import TestCaseRunner from '../test_case_runner'
import UserCodeRunner from '../../user_code_runner'
import { IdGenerator } from '@cucumber/messages'
import * as messages from '@cucumber/messages'
import TestRunHookDefinition from '../../models/test_run_hook_definition'
import { ISupportCodeLibrary } from '../../support_code_library_builder/types'
import { doesHaveValue, valueOrDefault } from '../../value_checker'
import { IRuntimeOptions } from '../index'
import { PredictableTestRunStopwatch, RealTestRunStopwatch } from '../stopwatch'
import { duration } from 'durations'
import { pathToFileURL } from 'url'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { importer } = require('../../importer')
const { uuid } = IdGenerator
type IExitFunction = (exitCode: number, error?: Error, message?: string) => void
type IMessageSender = (command: ICoordinatorReport) => void
export default class Worker {
private readonly cwd: string
private readonly exit: IExitFunction
private readonly id: string
private readonly eventBroadcaster: EventEmitter
private filterStacktraces: boolean
private readonly newId: IdGenerator.NewId
private readonly sendMessage: IMessageSender
private readonly stackTraceFilter: StackTraceFilter
private supportCodeLibrary: ISupportCodeLibrary
private worldParameters: any
private options: IRuntimeOptions
constructor({
cwd,
exit,
id,
sendMessage,
}: {
cwd: string
exit: IExitFunction
id: string
sendMessage: IMessageSender
}) {
this.id = id
this.newId = uuid()
this.cwd = cwd
this.exit = exit
this.sendMessage = sendMessage
this.eventBroadcaster = new EventEmitter()
this.stackTraceFilter = new StackTraceFilter()
this.eventBroadcaster.on('envelope', (envelope: messages.Envelope) => {
this.sendMessage({
jsonEnvelope: JSON.stringify(envelope),
})
})
}
async initialize({
filterStacktraces,
supportCodeRequiredModules,
supportCodePaths,
supportCodeIds,
options,
}: IWorkerCommandInitialize): Promise<void> {
supportCodeRequiredModules.map((module) => require(module))
supportCodeLibraryBuilder.reset(this.cwd, this.newId)
for (const codePath of supportCodePaths) {
if (supportCodeRequiredModules.length) {
require(codePath)
} else {
await importer(pathToFileURL(codePath))
}
}
this.supportCodeLibrary = supportCodeLibraryBuilder.finalize(supportCodeIds)
this.worldParameters = options.worldParameters
this.options = options
this.filterStacktraces = filterStacktraces
if (this.filterStacktraces) {
this.stackTraceFilter.filter()
}
await this.runTestRunHooks(
this.supportCodeLibrary.beforeTestRunHookDefinitions,
'a BeforeAll'
)
this.sendMessage({ ready: true })
}
async finalize(): Promise<void> {
await this.runTestRunHooks(
this.supportCodeLibrary.afterTestRunHookDefinitions,
'an AfterAll'
)
if (this.filterStacktraces) {
this.stackTraceFilter.unfilter()
}
this.exit(0)
}
async receiveMessage(message: IWorkerCommand): Promise<void> {
if (doesHaveValue(message.initialize)) {
await this.initialize(message.initialize)
} else if (message.finalize) {
await this.finalize()
} else if (doesHaveValue(message.run)) {
await this.runTestCase(message.run)
}
}
async runTestCase({
gherkinDocument,
pickle,
testCase,
elapsed,
retries,
skip,
}: IWorkerCommandRun): Promise<void> {
const stopwatch = this.options.predictableIds
? new PredictableTestRunStopwatch()
: new RealTestRunStopwatch()
stopwatch.from(duration(elapsed))
const testCaseRunner = new TestCaseRunner({
eventBroadcaster: this.eventBroadcaster,
stopwatch,
gherkinDocument,
newId: this.newId,
pickle,
testCase,
retries,
skip,
supportCodeLibrary: this.supportCodeLibrary,
worldParameters: this.worldParameters,
})
await testCaseRunner.run()
this.sendMessage({ ready: true })
}
async runTestRunHooks(
testRunHookDefinitions: TestRunHookDefinition[],
name: string
): Promise<void> {
if (this.options.dryRun) {
return
}
for (const hookDefinition of testRunHookDefinitions) {
const { error } = await UserCodeRunner.run({
argsArray: [],
fn: hookDefinition.code,
thisArg: null,
timeoutInMilliseconds: valueOrDefault(
hookDefinition.options.timeout,
this.supportCodeLibrary.defaultTimeout
),
})
if (doesHaveValue(error)) {
const location = formatLocation(hookDefinition)
this.exit(
1,
error,
`${name} hook errored on worker ${this.id}, process exiting: ${location}`
)
}
}
}
}