Skip to content

Commit 0d46a64

Browse files
authored
Emit messages for test run hooks (#2644)
1 parent 46233f9 commit 0d46a64

File tree

14 files changed

+390
-81
lines changed

14 files changed

+390
-81
lines changed

compatibility/cck_spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const CCK_FEATURES_PATH = 'node_modules/@cucumber/compatibility-kit/features'
1717
const CCK_IMPLEMENTATIONS_PATH = 'compatibility/features'
1818

1919
const UNSUPPORTED = [
20-
// we don't support global hooks messages yet
21-
'global-hooks',
2220
'global-hooks-attachments',
2321
'global-hooks-beforeall-error',
2422
'global-hooks-afterall-error',
@@ -102,9 +100,45 @@ describe('Cucumber Compatibility Kit', () => {
102100
})
103101
)
104102

105-
expect(actualMessages)
103+
expect(reorderEnvelopes(actualMessages))
106104
.excludingEvery(ignorableKeys)
107105
.to.deep.eq(expectedMessages)
108106
})
109107
}
110108
})
109+
110+
function reorderEnvelopes(
111+
envelopes: ReadonlyArray<Envelope>
112+
): ReadonlyArray<Envelope> {
113+
let testRunStartedEnvelope: Envelope
114+
let testCaseStartedEnvelope: Envelope
115+
116+
const result: Envelope[] = []
117+
const moveAfterTestRunStarted: Envelope[] = []
118+
119+
for (const envelope of envelopes) {
120+
if (envelope.testRunStarted) {
121+
testRunStartedEnvelope = envelope
122+
}
123+
if (envelope.testCaseStarted) {
124+
testCaseStartedEnvelope = envelope
125+
}
126+
127+
if (
128+
(envelope.testRunHookStarted || envelope.testRunHookFinished) &&
129+
!testCaseStartedEnvelope
130+
) {
131+
moveAfterTestRunStarted.push(envelope)
132+
} else {
133+
result.push(envelope)
134+
}
135+
}
136+
137+
result.splice(
138+
result.indexOf(testRunStartedEnvelope) + 1,
139+
0,
140+
...moveAfterTestRunStarted
141+
)
142+
143+
return result
144+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { When, BeforeAll, AfterAll } from '../../../src'
2+
3+
BeforeAll({}, function () {
4+
// no-op
5+
})
6+
7+
BeforeAll({}, function () {
8+
// no-op
9+
})
10+
11+
When('a step passes', function () {
12+
// no-op
13+
})
14+
15+
When('a step fails', function () {
16+
throw new Error('Exception in step')
17+
})
18+
19+
AfterAll({}, function () {
20+
// no-op
21+
})
22+
23+
AfterAll({}, function () {
24+
// no-op
25+
})

features/support/formatter_output_helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const ignorableKeys = [
111111
'pickleStepId',
112112
'stepDefinitionIds',
113113
'testRunStartedId',
114+
'testRunHookStartedId',
114115
'testCaseId',
115116
'testCaseStartedId',
116117
'testStepId',

src/runtime/coordinator.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Runtime } from './index'
88

99
export class Coordinator implements Runtime {
1010
constructor(
11+
private testRunStartedId: string,
1112
private eventBroadcaster: EventEmitter,
1213
private newId: IdGenerator.NewId,
1314
private sourcedPickles: ReadonlyArray<SourcedPickle>,
@@ -16,17 +17,15 @@ export class Coordinator implements Runtime {
1617
) {}
1718

1819
async run(): Promise<boolean> {
19-
const testRunStartedId = this.newId()
20-
2120
this.eventBroadcaster.emit('envelope', {
2221
testRunStarted: {
23-
id: testRunStartedId,
22+
id: this.testRunStartedId,
2423
timestamp: timestamp(),
2524
},
2625
} satisfies Envelope)
2726

2827
const assembledTestCases = await assembleTestCases(
29-
testRunStartedId,
28+
this.testRunStartedId,
3029
this.eventBroadcaster,
3130
this.newId,
3231
this.sourcedPickles,
@@ -37,7 +36,7 @@ export class Coordinator implements Runtime {
3736

3837
this.eventBroadcaster.emit('envelope', {
3938
testRunFinished: {
40-
testRunStartedId,
39+
testRunStartedId: this.testRunStartedId,
4140
timestamp: timestamp(),
4241
success,
4342
},

src/runtime/make_runtime.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,26 @@ export async function makeRuntime({
2727
supportCodeLibrary: SupportCodeLibrary
2828
options: IRunOptionsRuntime
2929
}): Promise<Runtime> {
30+
const testRunStartedId = newId()
3031
const adapter: RuntimeAdapter =
3132
options.parallel > 0
3233
? new ChildProcessAdapter(
34+
testRunStartedId,
3335
environment,
3436
logger,
3537
eventBroadcaster,
3638
options,
3739
supportCodeLibrary
3840
)
3941
: new InProcessAdapter(
42+
testRunStartedId,
4043
eventBroadcaster,
4144
newId,
4245
options,
4346
supportCodeLibrary
4447
)
4548
return new Coordinator(
49+
testRunStartedId,
4650
eventBroadcaster,
4751
newId,
4852
sourcedPickles,

src/runtime/parallel/adapter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class ChildProcessAdapter implements RuntimeAdapter {
4242
private readonly workers: Record<string, ManagedWorker> = {}
4343

4444
constructor(
45+
private readonly testRunStartedId: string,
4546
private readonly environment: IRunEnvironment,
4647
private readonly logger: ILogger,
4748
private readonly eventBroadcaster: EventEmitter,
@@ -112,6 +113,7 @@ export class ChildProcessAdapter implements RuntimeAdapter {
112113
})
113114
worker.process.send({
114115
type: 'INITIALIZE',
116+
testRunStartedId: this.testRunStartedId,
115117
supportCodeCoordinates: this.supportCodeLibrary.originalCoordinates,
116118
supportCodeIds: {
117119
stepDefinitionIds: this.supportCodeLibrary.stepDefinitions.map(
@@ -123,6 +125,10 @@ export class ChildProcessAdapter implements RuntimeAdapter {
123125
),
124126
afterTestCaseHookDefinitionIds:
125127
this.supportCodeLibrary.afterTestCaseHookDefinitions.map((h) => h.id),
128+
beforeTestRunHookDefinitionIds:
129+
this.supportCodeLibrary.beforeTestRunHookDefinitions.map((h) => h.id),
130+
afterTestRunHookDefinitionIds:
131+
this.supportCodeLibrary.afterTestRunHookDefinitions.map((h) => h.id),
126132
},
127133
options: this.options,
128134
} satisfies InitializeCommand)

src/runtime/parallel/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type CoordinatorToWorkerCommand =
1313

1414
export interface InitializeCommand {
1515
type: 'INITIALIZE'
16+
testRunStartedId: string
1617
supportCodeCoordinates: ISupportCodeCoordinates
1718
supportCodeIds: CanonicalSupportCodeIds
1819
options: RuntimeOptions

src/runtime/parallel/worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class ChildProcessWorker {
5454
}
5555

5656
async initialize({
57+
testRunStartedId,
5758
supportCodeCoordinates,
5859
supportCodeIds,
5960
options,
@@ -75,6 +76,7 @@ export class ChildProcessWorker {
7576

7677
this.options = options
7778
this.worker = new Worker(
79+
testRunStartedId,
7880
this.id,
7981
this.eventBroadcaster,
8082
this.newId,

src/runtime/run_test_run_hooks.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/runtime/serial/adapter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export class InProcessAdapter implements RuntimeAdapter {
1111
private failing: boolean = false
1212

1313
constructor(
14+
testRunStartedId: string,
1415
eventBroadcaster: EventEmitter,
1516
newId: IdGenerator.NewId,
1617
options: RuntimeOptions,
1718
supportCodeLibrary: SupportCodeLibrary
1819
) {
1920
this.worker = new Worker(
21+
testRunStartedId,
2022
undefined,
2123
eventBroadcaster,
2224
newId,

0 commit comments

Comments
 (0)