Skip to content

fix: mochawesome helper with unique screenshots #4959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
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
15 changes: 7 additions & 8 deletions lib/helper/Mochawesome.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
let addMochawesomeContext
let currentTest
let currentSuite

Expand All @@ -16,7 +15,8 @@ class Mochawesome extends Helper {
disableScreenshots: false,
}

addMochawesomeContext = require('mochawesome/addContext')
this._addContext = require('mochawesome/addContext')

this._createConfig(config)
}

Expand Down Expand Up @@ -44,28 +44,27 @@ class Mochawesome extends Helper {
if (this.options.disableScreenshots) return
let fileName
// Get proper name if we are fail on hook
if (test.ctx.test.type === 'hook') {
if (test.ctx?.test?.type === 'hook') {
currentTest = { test: test.ctx.test }
// ignore retries if we are in hook
test._retries = -1
fileName = clearString(`${test.title}_${currentTest.test.title}`)
} else {
currentTest = { test }
fileName = `${testToFileName(test)}`
fileName = testToFileName(test)
}
if (this.options.uniqueScreenshotNames) {
const uuid = test.uuid || test.ctx.test.uuid
fileName = `${fileName.substring(0, 10)}_${uuid}`
fileName = testToFileName(test, { unique: true })
}
if (test._retries < 1 || test._retries === test.retryNum) {
fileName = `${fileName}.failed.png`
return addMochawesomeContext(currentTest, fileName)
return this._addContext(currentTest, fileName)
}
}

addMochawesomeContext(context) {
if (currentTest === '') currentTest = { test: currentSuite.ctx.test }
return addMochawesomeContext(currentTest, context)
return this._addContext(currentTest, context)
}
}

Expand Down
13 changes: 12 additions & 1 deletion lib/mocha/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,19 @@ function cloneTest(test) {
return deserializeTest(serializeTest(test))
}

function testToFileName(test, suffix = '') {
/**
* Get a filename from the test object
* @param {CodeceptJS.Test} test
* @param {Object} options
* @param {string} options.suffix Add a suffix to the filename
* @param {boolean} options.unique Add a unique suffix to the file
*
* @returns {string} the filename
*/
function testToFileName(test, { suffix = '', unique = false } = {}) {
let fileName = test.title

if (unique) fileName = `${fileName}_${test?.uid || Math.floor(new Date().getTime() / 1000)}`
if (suffix) fileName = `${fileName}_${suffix}`
// remove tags with empty string (disable for now)
// fileName = fileName.replace(/\@\w+/g, '')
Expand All @@ -151,6 +161,7 @@ function testToFileName(test, suffix = '') {
// fileName = `${clearString(test.parent.title)}_${fileName}`
// }
fileName = clearString(fileName).slice(0, 100)

return fileName
}

Expand Down
10 changes: 1 addition & 9 deletions lib/plugin/screenshotOnFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = function (config) {
let fileName

if (options.uniqueScreenshotNames && test) {
fileName = `${testToFileName(test, _getUUID(test))}.failed.png`
fileName = `${testToFileName(test, { unique: true })}.failed.png`
} else {
fileName = `${testToFileName(test)}.failed.png`
}
Expand Down Expand Up @@ -137,12 +137,4 @@ module.exports = function (config) {
true,
)
})

function _getUUID(test) {
if (test.uid) {
return test.uid
}

return Math.floor(new Date().getTime() / 1000)
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
"figures": "3.2.0",
"fn-args": "4.0.0",
"fs-extra": "11.3.0",
"glob": ">=9.0.0 <12",
"fuse.js": "^7.0.0",
"glob": ">=9.0.0 <12",
"html-minifier-terser": "7.2.0",
"inquirer": "8.2.6",
"invisi-data": "^1.0.0",
Expand All @@ -106,6 +106,7 @@
"lodash.merge": "4.6.2",
"mkdirp": "3.0.1",
"mocha": "11.1.0",
"mochawesome": "^7.1.3",
"monocart-coverage-reports": "2.12.3",
"ms": "2.1.3",
"ora-classic": "5.4.2",
Expand Down
33 changes: 33 additions & 0 deletions test/unit/plugin/screenshotOnFail_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const event = require('../../../lib/event')
const recorder = require('../../../lib/recorder')
const { createTest } = require('../../../lib/mocha/test')
const { deserializeSuite } = require('../../../lib/mocha/suite')
const MochawesomeHelper = require('../../../lib/helper/Mochawesome')

let screenshotSaved

describe('screenshotOnFail', () => {
Expand Down Expand Up @@ -101,5 +103,36 @@ describe('screenshotOnFail', () => {
await recorder.promise()
expect(!screenshotSaved.called).is.ok
})

it('should have the same unique file name as the mochawesome helper when the uuid is present', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
const test = createTest('test1')
test.uid = '1234'

const helper = new MochawesomeHelper({ uniqueScreenshotNames: true })
const spy = sinon.spy(helper, '_addContext')
helper._failed(test)

event.dispatcher.emit(event.test.failed, test)
await recorder.promise()

const screenshotFileName = screenshotSaved.getCall(0).args[0]
expect(spy.getCall(0).args[1]).to.equal(screenshotFileName)
})

it('should have the same unique file name as the mochawesome helper when the uuid is not present', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
const test = createTest('test1')

const helper = new MochawesomeHelper({ uniqueScreenshotNames: true })
const spy = sinon.spy(helper, '_addContext')
helper._failed(test)

event.dispatcher.emit(event.test.failed, test)
await recorder.promise()

const screenshotFileName = screenshotSaved.getCall(0).args[0]
expect(spy.getCall(0).args[1]).to.equal(screenshotFileName)
})
// TODO: write more tests for different options
})