Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/mocha/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function serializeTest(test, error = null) {
const parent = {}
if (test.parent) {
parent.title = test.parent.title
parent.file = test.parent.file
}

if (test.opts) {
Expand Down
12 changes: 2 additions & 10 deletions lib/plugin/junitReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ export default function (config = {}) {

let written = false
const hookFailures = []
// groupBySuite() (below) groups by object identity, not by value — reused
// across BeforeSuite/AfterSuite failures from the same worker-forwarded
// suite so they land in one <testsuite>, not one per failure.
const workerSuiteByKey = new Map()

event.dispatcher.on(event.hook.failed, hook => {
if (!hook || !['BeforeSuite', 'AfterSuite'].includes(hook.hookName)) return
Expand All @@ -84,11 +80,7 @@ export default function (config = {}) {
// groups under its real suite instead of the "Tests" fallback.
let suite = runnable && runnable.parent
if (!suite && hook.suiteTitle) {
const key = `${hook.suiteTitle} ${hook.suiteFile || ''}`
if (!workerSuiteByKey.has(key)) {
workerSuiteByKey.set(key, { title: hook.suiteTitle, file: hook.suiteFile, tags: hook.suiteTags })
}
suite = workerSuiteByKey.get(key)
suite = { title: hook.suiteTitle, file: hook.suiteFile, tags: hook.suiteTags }
}
hookFailures.push({
title: hook.title || `${hook.hookName} hook failed`,
Expand Down Expand Up @@ -296,7 +288,7 @@ function groupBySuite(tests) {
const groups = []
const byKey = new Map()
for (const test of tests) {
const key = test.parent || test
const key = test.parent ? `${test.parent.title}\0${test.parent.file || test.file || ''}` : test
if (!byKey.has(key)) {
const list = []
byKey.set(key, list)
Expand Down
39 changes: 39 additions & 0 deletions test/unit/junitReporter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,45 @@ describe('JUnit Reporter Plugin', () => {
expect(tagsProp.$.value).to.equal('@smoke')
})

it('groups worker tests with separately deserialized parents into one testsuite per suite', async () => {
junitReporter({})

const workerTest = (suiteTitle, title) => ({ title, state: 'passed', duration: 10, parent: { title: suiteTitle }, meta: {}, steps: [] })

event.dispatcher.emit(event.workers.result, {
tests: [workerTest('SuiteB', 'scenario 1'), workerTest('SuiteB', 'scenario 2'), workerTest('SuiteA', 'scenario 1'), workerTest('SuiteA', 'scenario 2')],
stats: { start: new Date(), passes: 4, failures: 0, pending: 0, tests: 4 },
})

const parsed = await parseReport(tmpDir)
const suites = parsed.testsuites.testsuite
expect(suites.map(s => s.$.name)).to.deep.equal(['SuiteB', 'SuiteA'])
expect(suites.map(s => s.$.tests)).to.deep.equal(['2', '2'])
})

it('groups a worker suite hook failure with the tests of the same suite', async () => {
junitReporter({})

event.dispatcher.emit(event.hook.failed, {
hookName: 'AfterSuite',
title: '"after all" hook: AfterSuite for "scenario 1"',
suiteTitle: 'SuiteC',
suiteFile: '/tests/suite_c_test.js',
error: { message: 'AfterSuite failed' },
})

event.dispatcher.emit(event.workers.result, {
tests: [{ title: 'scenario 1', state: 'passed', duration: 10, parent: { title: 'SuiteC', file: '/tests/suite_c_test.js' }, meta: {}, steps: [] }],
stats: { start: new Date(), passes: 1, failures: 0, pending: 0, tests: 1 },
})

const parsed = await parseReport(tmpDir)
const suites = parsed.testsuites.testsuite
expect(suites).to.have.length(1)
expect(suites[0].$.name).to.equal('SuiteC')
expect(suites[0].$.tests).to.equal('2')
})

it('adds suite hook failures forwarded from workers as failed testcases', async () => {
junitReporter({})

Expand Down
Loading