Skip to content

Commit ab4ed74

Browse files
committed
add unit tests
1 parent f144430 commit ab4ed74

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

lib/helper/Mochawesome.js

+5-5
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,7 +44,7 @@ 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
@@ -58,13 +58,13 @@ class Mochawesome extends Helper {
5858
}
5959
if (test._retries < 1 || test._retries === test.retryNum) {
6060
fileName = `${fileName}.failed.png`
61-
return addMochawesomeContext(currentTest, fileName)
61+
return this._addContext(currentTest, fileName)
6262
}
6363
}
6464

6565
addMochawesomeContext(context) {
6666
if (currentTest === '') currentTest = { test: currentSuite.ctx.test }
67-
return addMochawesomeContext(currentTest, context)
67+
return this._addContext(currentTest, context)
6868
}
6969
}
7070

test/unit/plugin/screenshotOnFail_test.js

+36
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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+
1314
let screenshotSaved
1415

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

0 commit comments

Comments
 (0)