Skip to content

Commit 5205311

Browse files
committed
fix(junitReporter): stamp suites with their real start time
junitReporter writes each `<testsuite timestamp>` from `suite.startedAt`, but nothing ever set that field. Mocha's Suite does not carry it, and `startedAt` was only assigned to individual tests, in lib/listener/steps.js. So `toIso()` always fell through to its `new Date()` fallback and every suite was stamped with the moment the XML was serialized, which is after the suite (and its AfterSuite) finished. The steps listener now stamps the suite on `event.suite.before`, mirroring what it already does for tests. Note this covers the in-process run. Under `run-workers` the parent only receives `{ title }` for a test's parent suite, so the timestamp cannot survive that boundary without changing the worker payload. Closes #5668
1 parent 8b91815 commit 5205311

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

lib/listener/steps.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ const EXCLUDED_SESSIONS = ['tryTo', 'hopeThat']
1616
* Register steps inside tests
1717
*/
1818
export default function () {
19+
// Mocha's Suite has no start timestamp of its own, and junitReporter reads
20+
// `suite.startedAt` for each `<testsuite timestamp>`. Without this the
21+
// reporter falls back to `new Date()` at write time, stamping every suite
22+
// with the moment the XML was serialized. (#5668)
23+
event.dispatcher.on(event.suite.before, suite => {
24+
suite.startedAt = +new Date()
25+
})
26+
1927
event.dispatcher.on(event.test.before, test => {
2028
test.startedAt = +new Date()
2129
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect } from 'chai'
2+
import event from '../../../lib/event.js'
3+
import recorder from '../../../lib/recorder.js'
4+
5+
import stepsListener from '../../../lib/listener/steps.js'
6+
7+
// junitReporter reads `suite.startedAt` for each `<testsuite timestamp>`.
8+
// Mocha never sets it, so before this listener existed the reporter fell back
9+
// to `new Date()` at write time. (#5668)
10+
describe('Steps Listener - suite.startedAt', () => {
11+
beforeEach(() => {
12+
recorder.reset()
13+
recorder.start()
14+
event.cleanDispatcher()
15+
stepsListener()
16+
})
17+
18+
afterEach(() => {
19+
event.cleanDispatcher()
20+
recorder.reset()
21+
})
22+
23+
it('stamps the suite when it starts', () => {
24+
const suite = { title: 'Login' }
25+
const before = Date.now()
26+
27+
event.emit(event.suite.before, suite)
28+
29+
expect(suite.startedAt).to.be.a('number')
30+
expect(suite.startedAt).to.be.at.least(before)
31+
expect(suite.startedAt).to.be.at.most(Date.now())
32+
})
33+
34+
it('stamps each suite independently', () => {
35+
const first = { title: 'Login' }
36+
const second = { title: 'Dashboard' }
37+
38+
event.emit(event.suite.before, first)
39+
event.emit(event.suite.before, second)
40+
41+
expect(first.startedAt).to.be.a('number')
42+
expect(second.startedAt).to.be.a('number')
43+
expect(second.startedAt).to.be.at.least(first.startedAt)
44+
})
45+
})

0 commit comments

Comments
 (0)