-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
run_cucumber.ts
125 lines (115 loc) · 3.54 KB
/
run_cucumber.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
import { Envelope, IdGenerator, ParseError } from '@cucumber/messages'
import { EventEmitter } from 'events'
import { EventDataCollector } from '../formatter/helpers'
import { emitMetaMessage, emitSupportCodeMessages } from '../cli/helpers'
import { IRunOptions, IRunEnvironment, IRunResult } from './types'
import { resolvePaths } from './paths'
import { makeRuntime } from './runtime'
import { initializeFormatters } from './formatters'
import { getSupportCodeLibrary } from './support'
import { Console } from 'console'
import { mergeEnvironment } from './environment'
import { getFilteredPicklesAndErrors } from './gherkin'
/**
* Execute a Cucumber test run.
*
* @public
* @param configuration - Configuration loaded from `loadConfiguration`.
* @param environment - Project environment.
* @param onMessage - Callback fired each time Cucumber emits a message.
*/
export async function runCucumber(
configuration: IRunOptions,
environment: IRunEnvironment = {},
onMessage?: (message: Envelope) => void
): Promise<IRunResult> {
const { cwd, stdout, stderr, env } = mergeEnvironment(environment)
const logger = new Console(stdout, stderr)
const newId = IdGenerator.uuid()
const supportCoordinates =
'World' in configuration.support
? configuration.support.originalCoordinates
: configuration.support
const { unexpandedFeaturePaths, featurePaths, requirePaths, importPaths } =
await resolvePaths(cwd, configuration.sources, supportCoordinates)
const supportCodeLibrary =
'World' in configuration.support
? configuration.support
: await getSupportCodeLibrary({
cwd,
newId,
requirePaths,
importPaths,
requireModules: supportCoordinates.requireModules,
})
const eventBroadcaster = new EventEmitter()
if (onMessage) {
eventBroadcaster.on('envelope', onMessage)
}
const eventDataCollector = new EventDataCollector(eventBroadcaster)
let formatterStreamError = false
const cleanup = await initializeFormatters({
env,
cwd,
stdout,
stderr,
logger,
onStreamError: () => (formatterStreamError = true),
eventBroadcaster,
eventDataCollector,
configuration: configuration.formats,
supportCodeLibrary,
})
await emitMetaMessage(eventBroadcaster, env)
let pickleIds: string[] = []
let parseErrors: ParseError[] = []
if (featurePaths.length > 0) {
const gherkinResult = await getFilteredPicklesAndErrors({
newId,
cwd,
logger,
unexpandedFeaturePaths,
featurePaths,
coordinates: configuration.sources,
onEnvelope: (envelope) => eventBroadcaster.emit('envelope', envelope),
})
pickleIds = gherkinResult.filteredPickles.map(({ pickle }) => pickle.id)
parseErrors = gherkinResult.parseErrors
}
if (parseErrors.length) {
parseErrors.forEach((parseError) => {
logger.error(
`Parse error in "${parseError.source.uri}" ${parseError.message}`
)
})
await cleanup()
return {
success: false,
support: supportCodeLibrary,
}
}
emitSupportCodeMessages({
eventBroadcaster,
supportCodeLibrary,
newId,
})
const runtime = makeRuntime({
cwd,
logger,
eventBroadcaster,
eventDataCollector,
pickleIds,
newId,
supportCodeLibrary,
requireModules: supportCoordinates.requireModules,
requirePaths,
importPaths,
options: configuration.runtime,
})
const success = await runtime.start()
await cleanup()
return {
success: success && !formatterStreamError,
support: supportCodeLibrary,
}
}