Skip to content

Commit d6a25ce

Browse files
author
DavertMik
committed
fixed screenshot tests
1 parent e8a184f commit d6a25ce

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

lib/mocha/cli.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,25 @@ class Cli extends Base {
198198
// add new line before the message
199199
err.message = '\n ' + err.message
200200

201+
// explicitly show file with error
202+
if (test.file) {
203+
log += `\n${output.styles.basic(figures.circle)} ${output.styles.section('File:')} ${output.styles.basic(test.file)}\n`
204+
}
205+
201206
const steps = test.steps || (test.ctx && test.ctx.test.steps)
202207

203208
if (steps && steps.length) {
204209
let scenarioTrace = ''
205-
steps.reverse().forEach(step => {
206-
const hasFailed = step.status === 'failed'
207-
let line = `${hasFailed ? output.styles.bold(figures.cross) : figures.tick} ${step.toCode()} ${step.line()}`
208-
if (hasFailed) line = output.styles.bold(line)
209-
scenarioTrace += `\n${line}`
210-
})
211-
log += `${output.styles.basic(figures.circle)} ${output.styles.section('Scenario Steps')}:${scenarioTrace}\n`
210+
steps
211+
.reverse()
212+
.slice(0, 10)
213+
.forEach(step => {
214+
const hasFailed = step.status === 'failed'
215+
let line = `${hasFailed ? output.styles.bold(figures.cross) : figures.tick} ${step.toCode()} ${step.line()}`
216+
if (hasFailed) line = output.styles.bold(line)
217+
scenarioTrace += `\n${line}`
218+
})
219+
log += `\n${output.styles.basic(figures.circle)} ${output.styles.section('Scenario Steps')}:${scenarioTrace}\n`
212220
}
213221

214222
// display artifacts in debug mode

lib/plugin/pageInfo.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ module.exports = function (config = {}) {
6262
})
6363
recorder.add('HTML snapshot failed test', async () => {
6464
try {
65-
const currentOutputLevel = output.level()
66-
output.level(0)
6765
const html = await helper.grabHTMLFrom('body')
68-
output.level(currentOutputLevel)
6966

7067
if (!html) return
7168

lib/plugin/screenshotOnFail.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = function (config) {
6464
}
6565

6666
if (Codeceptjs.container.mocha()) {
67-
options.reportDir = Codeceptjs.container.mocha().options.reporterOptions && Codeceptjs.container.mocha().options.reporterOptions.reportDir
67+
options.reportDir = Codeceptjs.container.mocha()?.options?.reporterOptions && Codeceptjs.container.mocha()?.options?.reporterOptions?.reportDir
6868
}
6969

7070
if (options.disableScreenshots) {
@@ -73,7 +73,7 @@ module.exports = function (config) {
7373
}
7474

7575
event.dispatcher.on(event.test.failed, (test, _err, hookName) => {
76-
if (hookName == 'BeforeSuite' || hookName == 'AfterSuite') {
76+
if (hookName === 'BeforeSuite' || hookName === 'AfterSuite') {
7777
// no browser here
7878
return
7979
}

lib/plugin/stepByStepReport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module.exports = function (config) {
122122
})
123123

124124
event.dispatcher.on(event.test.failed, (test, _err, hookName) => {
125-
if (hookName == 'BeforeSuite' || hookName == 'AfterSuite') {
125+
if (hookName === 'BeforeSuite' || hookName === 'AfterSuite') {
126126
// no browser here
127127
return
128128
}

test/unit/plugin/screenshotOnFail_test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ describe('screenshotOnFail', () => {
4949

5050
it('should create screenshot with unique name', async () => {
5151
screenshotOnFail({ uniqueScreenshotNames: true })
52-
event.dispatcher.emit(event.test.failed, { title: 'test1', uuid: 1 })
52+
53+
const test = { title: 'test1', uid: 1 }
54+
event.dispatcher.emit(event.test.failed, test)
5355
await recorder.promise()
5456
expect(screenshotSaved.called).is.ok
55-
expect('test1_1.failed.png').is.equal(screenshotSaved.getCall(0).args[0])
57+
expect(`test1_${test.uid}.failed.png`).is.equal(screenshotSaved.getCall(0).args[0])
5658
})
5759

58-
it('should create screenshot with unique name when uuid is null', async () => {
60+
it('should create screenshot with unique name when uid is null', async () => {
5961
screenshotOnFail({ uniqueScreenshotNames: true })
62+
6063
event.dispatcher.emit(event.test.failed, { title: 'test1' })
6164
await recorder.promise()
6265
expect(screenshotSaved.called).is.ok
@@ -67,14 +70,14 @@ describe('screenshotOnFail', () => {
6770

6871
it('should not save screenshot in BeforeSuite', async () => {
6972
screenshotOnFail({ uniqueScreenshotNames: true })
70-
event.dispatcher.emit(event.test.failed, { title: 'test1', ctx: { _runnable: { title: 'hook: BeforeSuite' } } })
73+
event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'BeforeSuite')
7174
await recorder.promise()
7275
expect(!screenshotSaved.called).is.ok
7376
})
7477

7578
it('should not save screenshot in AfterSuite', async () => {
7679
screenshotOnFail({ uniqueScreenshotNames: true })
77-
event.dispatcher.emit(event.test.failed, { title: 'test1', ctx: { _runnable: { title: 'hook: AfterSuite' } } })
80+
event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'AfterSuite')
7881
await recorder.promise()
7982
expect(!screenshotSaved.called).is.ok
8083
})

0 commit comments

Comments
 (0)