Skip to content

Commit 60dbffe

Browse files
Lando1nkobenguyent
andauthored
fix: mochawesome helper with unique screenshots (#4959)
* fix: mochawesome helper with unique screenshots * add unit tests * install mochawesome --------- Co-authored-by: kobenguyent <[email protected]>
1 parent 256e525 commit 60dbffe

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

lib/helper/Mochawesome.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
let addMochawesomeContext
21
let currentTest
32
let currentSuite
43

@@ -16,7 +15,8 @@ class Mochawesome extends Helper {
1615
disableScreenshots: false,
1716
}
1817

19-
addMochawesomeContext = require('mochawesome/addContext')
18+
this._addContext = require('mochawesome/addContext')
19+
2020
this._createConfig(config)
2121
}
2222

@@ -44,28 +44,27 @@ class Mochawesome extends Helper {
4444
if (this.options.disableScreenshots) return
4545
let fileName
4646
// Get proper name if we are fail on hook
47-
if (test.ctx.test.type === 'hook') {
47+
if (test.ctx?.test?.type === 'hook') {
4848
currentTest = { test: test.ctx.test }
4949
// ignore retries if we are in hook
5050
test._retries = -1
5151
fileName = clearString(`${test.title}_${currentTest.test.title}`)
5252
} else {
5353
currentTest = { test }
54-
fileName = `${testToFileName(test)}`
54+
fileName = testToFileName(test)
5555
}
5656
if (this.options.uniqueScreenshotNames) {
57-
const uuid = test.uuid || test.ctx.test.uuid
58-
fileName = `${fileName.substring(0, 10)}_${uuid}`
57+
fileName = testToFileName(test, { unique: true })
5958
}
6059
if (test._retries < 1 || test._retries === test.retryNum) {
6160
fileName = `${fileName}.failed.png`
62-
return addMochawesomeContext(currentTest, fileName)
61+
return this._addContext(currentTest, fileName)
6362
}
6463
}
6564

6665
addMochawesomeContext(context) {
6766
if (currentTest === '') currentTest = { test: currentSuite.ctx.test }
68-
return addMochawesomeContext(currentTest, context)
67+
return this._addContext(currentTest, context)
6968
}
7069
}
7170

lib/mocha/test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,19 @@ function cloneTest(test) {
135135
return deserializeTest(serializeTest(test))
136136
}
137137

138-
function testToFileName(test, suffix = '') {
138+
/**
139+
* Get a filename from the test object
140+
* @param {CodeceptJS.Test} test
141+
* @param {Object} options
142+
* @param {string} options.suffix Add a suffix to the filename
143+
* @param {boolean} options.unique Add a unique suffix to the file
144+
*
145+
* @returns {string} the filename
146+
*/
147+
function testToFileName(test, { suffix = '', unique = false } = {}) {
139148
let fileName = test.title
140149

150+
if (unique) fileName = `${fileName}_${test?.uid || Math.floor(new Date().getTime() / 1000)}`
141151
if (suffix) fileName = `${fileName}_${suffix}`
142152
// remove tags with empty string (disable for now)
143153
// fileName = fileName.replace(/\@\w+/g, '')
@@ -151,6 +161,7 @@ function testToFileName(test, suffix = '') {
151161
// fileName = `${clearString(test.parent.title)}_${fileName}`
152162
// }
153163
fileName = clearString(fileName).slice(0, 100)
164+
154165
return fileName
155166
}
156167

lib/plugin/screenshotOnFail.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = function (config) {
8686
let fileName
8787

8888
if (options.uniqueScreenshotNames && test) {
89-
fileName = `${testToFileName(test, _getUUID(test))}.failed.png`
89+
fileName = `${testToFileName(test, { unique: true })}.failed.png`
9090
} else {
9191
fileName = `${testToFileName(test)}.failed.png`
9292
}
@@ -137,12 +137,4 @@ module.exports = function (config) {
137137
true,
138138
)
139139
})
140-
141-
function _getUUID(test) {
142-
if (test.uid) {
143-
return test.uid
144-
}
145-
146-
return Math.floor(new Date().getTime() / 1000)
147-
}
148140
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595
"figures": "3.2.0",
9696
"fn-args": "4.0.0",
9797
"fs-extra": "11.3.0",
98-
"glob": ">=9.0.0 <12",
9998
"fuse.js": "^7.0.0",
99+
"glob": ">=9.0.0 <12",
100100
"html-minifier-terser": "7.2.0",
101101
"inquirer": "8.2.6",
102102
"invisi-data": "^1.0.0",
@@ -153,6 +153,7 @@
153153
"jsdoc": "^3.6.11",
154154
"jsdoc-typeof-plugin": "1.0.0",
155155
"json-server": "0.17.4",
156+
"mochawesome": "^7.1.3",
156157
"playwright": "1.52.0",
157158
"prettier": "^3.3.2",
158159
"puppeteer": "24.8.0",

test/unit/plugin/screenshotOnFail_test.js

+33
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const event = require('../../../lib/event')
1010
const recorder = require('../../../lib/recorder')
1111
const { createTest } = require('../../../lib/mocha/test')
1212
const { deserializeSuite } = require('../../../lib/mocha/suite')
13+
const MochawesomeHelper = require('../../../lib/helper/Mochawesome')
14+
1315
let screenshotSaved
1416

1517
describe('screenshotOnFail', () => {
@@ -101,5 +103,36 @@ describe('screenshotOnFail', () => {
101103
await recorder.promise()
102104
expect(!screenshotSaved.called).is.ok
103105
})
106+
107+
it('should have the same unique file name as the mochawesome helper when the uuid is present', async () => {
108+
screenshotOnFail({ uniqueScreenshotNames: true })
109+
const test = createTest('test1')
110+
test.uid = '1234'
111+
112+
const helper = new MochawesomeHelper({ uniqueScreenshotNames: true })
113+
const spy = sinon.spy(helper, '_addContext')
114+
helper._failed(test)
115+
116+
event.dispatcher.emit(event.test.failed, test)
117+
await recorder.promise()
118+
119+
const screenshotFileName = screenshotSaved.getCall(0).args[0]
120+
expect(spy.getCall(0).args[1]).to.equal(screenshotFileName)
121+
})
122+
123+
it('should have the same unique file name as the mochawesome helper when the uuid is not present', async () => {
124+
screenshotOnFail({ uniqueScreenshotNames: true })
125+
const test = createTest('test1')
126+
127+
const helper = new MochawesomeHelper({ uniqueScreenshotNames: true })
128+
const spy = sinon.spy(helper, '_addContext')
129+
helper._failed(test)
130+
131+
event.dispatcher.emit(event.test.failed, test)
132+
await recorder.promise()
133+
134+
const screenshotFileName = screenshotSaved.getCall(0).args[0]
135+
expect(spy.getCall(0).args[1]).to.equal(screenshotFileName)
136+
})
104137
// TODO: write more tests for different options
105138
})

0 commit comments

Comments
 (0)