Skip to content

Commit b62dd35

Browse files
author
DavertMik
committed
added notes & metadata for tests, improved output
1 parent c561dc9 commit b62dd35

File tree

13 files changed

+293
-190
lines changed

13 files changed

+293
-190
lines changed

docs/ai.md

Lines changed: 88 additions & 95 deletions
Large diffs are not rendered by default.

lib/command/run-workers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = async function (workerCount, selectedRuns, options) {
3030
output.print(`CodeceptJS v${require('../codecept').version()} ${output.standWithUkraine()}`)
3131
output.print(`Running tests in ${output.styles.bold(numberOfWorkers)} workers...`)
3232
output.print()
33+
store.hasWorkers = true
3334

3435
const workers = new Workers(numberOfWorkers, config)
3536
workers.overrideConfig(overrideConfigs)
@@ -100,7 +101,6 @@ module.exports = async function (workerCount, selectedRuns, options) {
100101
if (options.verbose || options.debug) store.debugMode = true
101102

102103
if (options.verbose) {
103-
global.debugMode = true
104104
const { getMachineInfo } = require('./info')
105105
await getMachineInfo()
106106
}

lib/heal.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ class Heal {
130130
})
131131

132132
if (failureContext?.test) {
133+
const test = failureContext.test
133134
let note = `This test was healed by '${suggestion.name}'`
134135
note += `\n\nReplace the failed code:\n\n`
135136
note += colors.red(`- ${failedStep.toCode()}\n`)
136137
note += colors.green(`+ ${codeSnippet}\n`)
137-
failureContext.notes.push(note)
138-
failureContext.test.meta.healed = true
138+
test.addNote('heal', note)
139+
test.meta.healed = true
139140
}
140141

141142
recorder.add('healed', () => output.print(colors.bold.green(` Code healed successfully by ${suggestion.name}`), colors.gray('(no errors thrown)')))

lib/helper/Playwright.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ class Playwright extends Helper {
482482

483483
async _before(test) {
484484
this.currentRunningTest = test
485+
485486
recorder.retry({
486487
retries: process.env.FAILED_STEP_RETRIES || 3,
487488
when: err => {
@@ -552,6 +553,15 @@ class Playwright extends Helper {
552553

553554
await this._setPage(mainPage)
554555

556+
try {
557+
// set metadata for reporting
558+
test.meta.browser = this.browser.browserType().name()
559+
test.meta.browserVersion = this.browser.version()
560+
test.meta.windowSize = `${this.page.viewportSize().width}x${this.page.viewportSize().height}`
561+
} catch (e) {
562+
this.debug('Failed to set metadata for reporting')
563+
}
564+
555565
if (this.options.trace) await this.browserContext.tracing.start({ screenshots: true, snapshots: true })
556566

557567
return this.browser

lib/mocha/cli.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {
22
reporters: { Base },
33
} = require('mocha')
4+
const figures = require('figures')
45
const ms = require('ms')
56
const event = require('../event')
67
const AssertionFailedError = require('../assert/error')
@@ -163,33 +164,49 @@ class Cli extends Base {
163164
const err = test.err
164165

165166
let log = ''
167+
let originalMessage = err.message
166168

167169
if (err instanceof AssertionFailedError) {
168170
err.message = err.inspect()
169171
}
170172

173+
// multi-line error messages
174+
err.message = '\n ' + (err.message || '').replace(/^/gm, ' ').trim()
175+
171176
const steps = test.steps || (test.ctx && test.ctx.test.steps)
172177

173178
if (steps && steps.length) {
174179
let scenarioTrace = ''
175180
steps.reverse().forEach(step => {
176-
const line = `- ${step.toCode()} ${step.line()}`
177-
// if (step.status === 'failed') line = '' + line;
181+
const hasFailed = step.status === 'failed'
182+
let line = `${hasFailed ? output.styles.bold(figures.cross) : figures.tick} ${step.toCode()} ${step.line()}`
183+
if (hasFailed) line = output.styles.bold(line)
178184
scenarioTrace += `\n${line}`
179185
})
180-
log += `${output.styles.bold('Scenario Steps')}:${scenarioTrace}\n`
186+
log += `${output.styles.basic(figures.circle)} ${output.styles.section('Scenario Steps')}:${scenarioTrace}\n`
181187
}
182188

183189
// display artifacts in debug mode
184190
if (test?.artifacts && Object.keys(test.artifacts).length) {
185-
log += `\n${output.styles.bold('Artifacts:')}`
191+
log += `\n${output.styles.basic(figures.circle)} ${output.styles.section('Artifacts:')}`
186192
for (const artifact of Object.keys(test.artifacts)) {
187193
log += `\n- ${artifact}: ${test.artifacts[artifact]}`
188194
}
189195
}
190196

197+
// display metadata
198+
if (test.meta && Object.keys(test.meta).length) {
199+
log += `\n\n${output.styles.basic(figures.circle)} ${output.styles.section('Metadata:')}`
200+
for (const [key, value] of Object.entries(test.meta)) {
201+
log += `\n- ${key}: ${value}`
202+
}
203+
}
204+
191205
try {
192-
let stack = err.stack ? err.stack.split('\n') : []
206+
let stack = err.stack
207+
stack = stack.replace(originalMessage, '')
208+
stack = stack ? stack.split('\n') : []
209+
193210
if (stack[0] && stack[0].includes(err.message)) {
194211
stack.shift()
195212
}

lib/mocha/featureConfig.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ class FeatureConfig {
77
this.suite = suite
88
}
99

10+
/**
11+
* Set metadata for this suite
12+
* @param {string} key
13+
* @param {string} value
14+
* @returns {this}
15+
*/
16+
meta(key, value) {
17+
this.suite.tests.forEach(test => {
18+
test.meta[key] = value
19+
})
20+
return this
21+
}
22+
1023
/**
1124
* Retry this test for number of times
1225
*

lib/mocha/scenarioConfig.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ class ScenarioConfig {
4242
return this
4343
}
4444

45+
/**
46+
* Set metadata for this test
47+
* @param {string} key
48+
* @param {string} value
49+
* @returns {this}
50+
*/
51+
meta(key, value) {
52+
this.test.meta[key] = value
53+
return this
54+
}
55+
4556
/**
4657
* Set timeout for this test
4758
* @param {number} timeout

lib/mocha/test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Test = require('mocha/lib/test')
2+
const Suite = require('mocha/lib/suite')
23
const { test: testWrapper } = require('./asyncWrapper')
34
const { enhanceMochaSuite } = require('./suite')
45

@@ -31,6 +32,11 @@ function enhanceMochaTest(test) {
3132
test.inject = {}
3233
test.opts = {}
3334

35+
test.notes = []
36+
test.addNote = (type, note) => {
37+
test.notes.push({ type, text: note })
38+
}
39+
3440
// Add new methods
3541
/**
3642
* @param {Mocha.Suite} suite - The Mocha suite to add this test to
@@ -41,20 +47,27 @@ function enhanceMochaTest(test) {
4147
test.tags = [...(test.tags || []), ...(suite.tags || [])]
4248
test.fullTitle = () => `${suite.title}: ${test.title}`
4349
test.meta = {}
44-
test.notes = []
4550
}
4651

4752
test.applyOptions = function (opts) {
4853
if (!opts) opts = {}
4954
test.opts = opts
55+
test.meta = opts.meta || {}
5056
test.totalTimeout = opts.timeout
5157
if (opts.retries) this.retries(opts.retries)
5258
}
5359

5460
return test
5561
}
5662

63+
function repackTestForWorkersTransport(test) {
64+
test = Object.assign(new Test(test.title || '', () => {}), test)
65+
test.parent = Object.assign(new Suite(test.parent.title), test.parent)
66+
return test
67+
}
68+
5769
module.exports = {
5870
createTest,
5971
enhanceMochaTest,
72+
repackTestForWorkersTransport,
6073
}

lib/mocha/types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ declare global {
88
tags: string[]
99
steps: string[]
1010
meta: Record<string, any>
11-
notes: string[]
11+
notes: Array<{
12+
type: string
13+
text: string
14+
}>
1215
config: Record<string, any>
1316
artifacts: string[]
1417
inject: Record<string, any>
@@ -17,6 +20,7 @@ declare global {
1720
totalTimeout?: number
1821
addToSuite(suite: Mocha.Suite): void
1922
applyOptions(opts: Record<string, any>): void
23+
addNote(type: string, note: string): void
2024
codeceptjs: boolean
2125
}
2226

0 commit comments

Comments
 (0)