Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed screenshot tests
  • Loading branch information
DavertMik committed Jan 26, 2025
commit d6a25ce140082f545a615939ffe064fa2c9570a8
22 changes: 15 additions & 7 deletions lib/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,25 @@ class Cli extends Base {
// add new line before the message
err.message = '\n ' + err.message

// explicitly show file with error
if (test.file) {
log += `\n${output.styles.basic(figures.circle)} ${output.styles.section('File:')} ${output.styles.basic(test.file)}\n`
}

const steps = test.steps || (test.ctx && test.ctx.test.steps)

if (steps && steps.length) {
let scenarioTrace = ''
steps.reverse().forEach(step => {
const hasFailed = step.status === 'failed'
let line = `${hasFailed ? output.styles.bold(figures.cross) : figures.tick} ${step.toCode()} ${step.line()}`
if (hasFailed) line = output.styles.bold(line)
scenarioTrace += `\n${line}`
})
log += `${output.styles.basic(figures.circle)} ${output.styles.section('Scenario Steps')}:${scenarioTrace}\n`
steps
.reverse()
.slice(0, 10)
.forEach(step => {
const hasFailed = step.status === 'failed'
let line = `${hasFailed ? output.styles.bold(figures.cross) : figures.tick} ${step.toCode()} ${step.line()}`
if (hasFailed) line = output.styles.bold(line)
scenarioTrace += `\n${line}`
})
log += `\n${output.styles.basic(figures.circle)} ${output.styles.section('Scenario Steps')}:${scenarioTrace}\n`
}

// display artifacts in debug mode
Expand Down
3 changes: 0 additions & 3 deletions lib/plugin/pageInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ module.exports = function (config = {}) {
})
recorder.add('HTML snapshot failed test', async () => {
try {
const currentOutputLevel = output.level()
output.level(0)
const html = await helper.grabHTMLFrom('body')
output.level(currentOutputLevel)

if (!html) return

Expand Down
4 changes: 2 additions & 2 deletions lib/plugin/screenshotOnFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function (config) {
}

if (Codeceptjs.container.mocha()) {
options.reportDir = Codeceptjs.container.mocha().options.reporterOptions && Codeceptjs.container.mocha().options.reporterOptions.reportDir
options.reportDir = Codeceptjs.container.mocha()?.options?.reporterOptions && Codeceptjs.container.mocha()?.options?.reporterOptions?.reportDir
}

if (options.disableScreenshots) {
Expand All @@ -73,7 +73,7 @@ module.exports = function (config) {
}

event.dispatcher.on(event.test.failed, (test, _err, hookName) => {
if (hookName == 'BeforeSuite' || hookName == 'AfterSuite') {
if (hookName === 'BeforeSuite' || hookName === 'AfterSuite') {
// no browser here
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/stepByStepReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = function (config) {
})

event.dispatcher.on(event.test.failed, (test, _err, hookName) => {
if (hookName == 'BeforeSuite' || hookName == 'AfterSuite') {
if (hookName === 'BeforeSuite' || hookName === 'AfterSuite') {
// no browser here
return
}
Expand Down
13 changes: 8 additions & 5 deletions test/unit/plugin/screenshotOnFail_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ describe('screenshotOnFail', () => {

it('should create screenshot with unique name', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
event.dispatcher.emit(event.test.failed, { title: 'test1', uuid: 1 })

const test = { title: 'test1', uid: 1 }
event.dispatcher.emit(event.test.failed, test)
await recorder.promise()
expect(screenshotSaved.called).is.ok
expect('test1_1.failed.png').is.equal(screenshotSaved.getCall(0).args[0])
expect(`test1_${test.uid}.failed.png`).is.equal(screenshotSaved.getCall(0).args[0])
})

it('should create screenshot with unique name when uuid is null', async () => {
it('should create screenshot with unique name when uid is null', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })

event.dispatcher.emit(event.test.failed, { title: 'test1' })
await recorder.promise()
expect(screenshotSaved.called).is.ok
Expand All @@ -67,14 +70,14 @@ describe('screenshotOnFail', () => {

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

it('should not save screenshot in AfterSuite', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
event.dispatcher.emit(event.test.failed, { title: 'test1', ctx: { _runnable: { title: 'hook: AfterSuite' } } })
event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'AfterSuite')
await recorder.promise()
expect(!screenshotSaved.called).is.ok
})
Expand Down
Loading