Skip to content

Commit 76fc494

Browse files
committed
add unit tests
1 parent f144430 commit 76fc494

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-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

+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)