Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/listener/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const EXCLUDED_SESSIONS = ['tryTo', 'hopeThat']
* Register steps inside tests
*/
export default function () {
// Mocha's Suite has no start timestamp of its own, and junitReporter reads
// `suite.startedAt` for each `<testsuite timestamp>`. Without this the
// reporter falls back to `new Date()` at write time, stamping every suite
// with the moment the XML was serialized. (#5668)
event.dispatcher.on(event.suite.before, suite => {
suite.startedAt = +new Date()
})

event.dispatcher.on(event.test.before, test => {
test.startedAt = +new Date()
})
Expand Down
45 changes: 45 additions & 0 deletions test/unit/listener/steps_suite_started_at_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai'
import event from '../../../lib/event.js'
import recorder from '../../../lib/recorder.js'

import stepsListener from '../../../lib/listener/steps.js'

// junitReporter reads `suite.startedAt` for each `<testsuite timestamp>`.
// Mocha never sets it, so before this listener existed the reporter fell back
// to `new Date()` at write time. (#5668)
describe('Steps Listener - suite.startedAt', () => {
beforeEach(() => {
recorder.reset()
recorder.start()
event.cleanDispatcher()
stepsListener()
})

afterEach(() => {
event.cleanDispatcher()
recorder.reset()
})

it('stamps the suite when it starts', () => {
const suite = { title: 'Login' }
const before = Date.now()

event.emit(event.suite.before, suite)

expect(suite.startedAt).to.be.a('number')
expect(suite.startedAt).to.be.at.least(before)
expect(suite.startedAt).to.be.at.most(Date.now())
})

it('stamps each suite independently', () => {
const first = { title: 'Login' }
const second = { title: 'Dashboard' }

event.emit(event.suite.before, first)
event.emit(event.suite.before, second)

expect(first.startedAt).to.be.a('number')
expect(second.startedAt).to.be.a('number')
expect(second.startedAt).to.be.at.least(first.startedAt)
})
})
Loading