diff --git a/lib/helper/Mochawesome.js b/lib/helper/Mochawesome.js index 9c20cd1d7..0f45ff723 100644 --- a/lib/helper/Mochawesome.js +++ b/lib/helper/Mochawesome.js @@ -1,4 +1,3 @@ -let addMochawesomeContext let currentTest let currentSuite @@ -16,7 +15,8 @@ class Mochawesome extends Helper { disableScreenshots: false, } - addMochawesomeContext = require('mochawesome/addContext') + this._addContext = require('mochawesome/addContext') + this._createConfig(config) } @@ -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) } } diff --git a/lib/mocha/test.js b/lib/mocha/test.js index 14c202085..7ff53721d 100644 --- a/lib/mocha/test.js +++ b/lib/mocha/test.js @@ -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, '') @@ -151,6 +161,7 @@ function testToFileName(test, suffix = '') { // fileName = `${clearString(test.parent.title)}_${fileName}` // } fileName = clearString(fileName).slice(0, 100) + return fileName } diff --git a/lib/plugin/screenshotOnFail.js b/lib/plugin/screenshotOnFail.js index aeba20834..04e855f92 100644 --- a/lib/plugin/screenshotOnFail.js +++ b/lib/plugin/screenshotOnFail.js @@ -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` } @@ -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) - } } diff --git a/package.json b/package.json index 112de68e7..e2d0c0c92 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/unit/plugin/screenshotOnFail_test.js b/test/unit/plugin/screenshotOnFail_test.js index 1cda0ec3f..a7ea44819 100644 --- a/test/unit/plugin/screenshotOnFail_test.js +++ b/test/unit/plugin/screenshotOnFail_test.js @@ -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', () => { @@ -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 })