Skip to content

Commit 7afcb4a

Browse files
committed
keep result.json on empty runs, record tests of suites with failed BeforeSuite
1 parent a1d3129 commit 7afcb4a

6 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎lib/codecept.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class Codecept {
351351
event.emit(event.all.after, this)
352352
// Wait for any recorder tasks added by event.all.after handlers
353353
await recorder.promise()
354-
if (!this.opts.child && !store.dryRun) container.result().save()
354+
if (!this.opts.child && !store.dryRun && container.result().tests.length) container.result().save()
355355
// Set exit code based on test failures
356356
if (failures) {
357357
process.exitCode = 1

‎lib/listener/result.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ export default function () {
99
event.dispatcher.on(event.test.before, test => {
1010
container.result().addTest(test)
1111
})
12+
13+
event.dispatcher.on(event.test.failed, test => {
14+
container.result().addTest(test)
15+
})
1216
}

‎lib/workers.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ class Workers extends EventEmitter {
915915

916916
this.emit(event.all.result, Container.result())
917917
event.dispatcher.emit(event.workers.result, Container.result())
918-
Container.result().save()
918+
if (Container.result().tests.length) Container.result().save()
919919
this.emit('end') // internal event
920920
}
921921

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const config = {
2+
tests: './*_test.js',
3+
output: './output',
4+
helpers: {},
5+
include: {},
6+
bootstrap: null,
7+
mocha: {},
8+
name: 'last-failed-hooks',
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from 'assert'
2+
3+
Feature('Hooks')
4+
5+
BeforeSuite(() => {
6+
assert.equal(process.env.LAST_FAILED_HOOKS_OK, '1')
7+
})
8+
9+
Scenario('prepares data', () => {
10+
console.log('executed: prepares data')
11+
})
12+
13+
Scenario('serves requests @smoke', () => {
14+
console.log('executed: serves requests')
15+
})

‎test/runner/last_failed_test.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@ const config = `--config ${codecept_dir}/codecept.conf.js`
1313
const outputDir = path.join(codecept_dir, 'output')
1414
const resultFile = path.join(outputDir, 'result.json')
1515

16+
const hooks_dir = path.join(__dirname, '/../data/sandbox/configs/last-failed-hooks')
17+
const hooksConfig = `--config ${hooks_dir}/codecept.conf.js`
18+
const hooksResultFile = path.join(hooks_dir, 'output', 'result.json')
19+
1620
const run = (command, env = {}) =>
1721
new Promise(resolve => {
1822
exec(`${runner} ${command} ${config}`, { env: { ...process.env, LAST_FAILED_FIXED: '', ...env } }, (err, stdout) => {
1923
resolve({ code: err ? err.code : 0, stdout })
2024
})
2125
})
2226

27+
const runHooks = (command, env = {}) =>
28+
new Promise(resolve => {
29+
exec(`${runner} ${command} ${hooksConfig}`, { env: { ...process.env, LAST_FAILED_HOOKS_OK: '', ...env } }, (err, stdout) => {
30+
resolve({ code: err ? err.code : 0, stdout })
31+
})
32+
})
33+
2334
const reportedTests = () => {
2435
const { tests } = JSON.parse(fs.readFileSync(resultFile, 'utf8'))
2536
return Object.fromEntries(tests.map(test => [test.title, test.state]))
@@ -37,6 +48,7 @@ describe('--last-failed', function () {
3748

3849
beforeEach(() => {
3950
fs.rmSync(outputDir, { recursive: true, force: true })
51+
fs.rmSync(path.join(hooks_dir, 'output'), { recursive: true, force: true })
4052
})
4153

4254
it('should save a report with test states after a run', async () => {
@@ -137,4 +149,51 @@ describe('--last-failed', function () {
137149
expect(stdout).not.toContain('passed')
138150
}
139151
})
152+
153+
it('should not wipe result.json when a run executes no tests', async () => {
154+
await run('run')
155+
expect(reportedTests()['multiplies numbers']).toEqual('failed')
156+
157+
const { stdout } = await run('run --grep "no such test"')
158+
expect(executedTests(stdout)).toEqual([])
159+
160+
expect(reportedTests()['multiplies numbers']).toEqual('failed')
161+
const { stdout: rerun } = await run('run --last-failed')
162+
expect(executedTests(rerun)).toEqual(['divides numbers', 'multiplies numbers', 'uppercases strings'])
163+
})
164+
165+
it('should not wipe result.json in workers when a run executes no tests', async () => {
166+
await run('run-workers 2')
167+
expect(reportedTests()['multiplies numbers']).toEqual('failed')
168+
169+
const { stdout } = await run('run-workers 2 --grep "no such test"')
170+
expect(executedTests(stdout)).toEqual([])
171+
172+
expect(reportedTests()['multiplies numbers']).toEqual('failed')
173+
})
174+
175+
it('should rerun tests of a suite with a failed BeforeSuite', async () => {
176+
const { code } = await runHooks('run')
177+
expect(code).toEqual(1)
178+
const { tests } = JSON.parse(fs.readFileSync(hooksResultFile, 'utf8'))
179+
expect(Object.fromEntries(tests.map(test => [test.title, test.state]))).toEqual({
180+
'prepares data': 'failed',
181+
'serves requests @smoke': 'failed',
182+
})
183+
184+
const { stdout } = await runHooks('run --last-failed')
185+
expect(executedTests(stdout)).toEqual(['prepares data', 'serves requests'])
186+
})
187+
188+
it('should rerun tests of a suite with a failed BeforeSuite in workers', async () => {
189+
await runHooks('run-workers 2')
190+
const { tests } = JSON.parse(fs.readFileSync(hooksResultFile, 'utf8'))
191+
expect(Object.fromEntries(tests.map(test => [test.title, test.state]))).toEqual({
192+
'prepares data': 'failed',
193+
'serves requests @smoke': 'failed',
194+
})
195+
196+
const { stdout } = await runHooks('run-workers 2 --last-failed')
197+
expect(executedTests(stdout)).toEqual(['prepares data', 'serves requests'])
198+
})
140199
})

0 commit comments

Comments
 (0)