Skip to content

Commit d031ad0

Browse files
authored
feat(cli): print failed hooks (#4476)
* feat(cli): print failed hooks info * feat(cli): print failed hooks info * feat(cli): print failed hooks info * fix: failed UTs * fix: failed UTs
1 parent 593129c commit d031ad0

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

lib/cli.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class Cli extends Base {
145145

146146
result() {
147147
const stats = this.stats;
148+
stats.failedHooks = 0;
148149
console.log();
149150

150151
// passes
@@ -216,8 +217,14 @@ class Cli extends Base {
216217
console.log();
217218
}
218219

220+
this.failures.forEach((failure) => {
221+
if (failure.constructor.name === 'Hook') {
222+
stats.failures -= stats.failures
223+
stats.failedHooks += 1
224+
}
225+
})
219226
event.emit(event.all.failures, { failuresLog, stats });
220-
output.result(stats.passes, stats.failures, stats.pending, ms(stats.duration));
227+
output.result(stats.passes, stats.failures, stats.pending, ms(stats.duration), stats.failedHooks);
221228

222229
if (stats.failures && output.level() < 3) {
223230
output.print(output.styles.debug('Run with --verbose flag to see complete NodeJS stacktrace'));

lib/command/workers/runTests.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ function collectStats() {
264264
event.dispatcher.on(event.test.passed, () => {
265265
stats.passes++;
266266
});
267-
event.dispatcher.on(event.test.failed, () => {
267+
event.dispatcher.on(event.test.failed, (test) => {
268+
if (test.ctx._runnable.title.includes('hook: AfterSuite')) {
269+
stats.failedHooks += 1;
270+
}
268271
stats.failures++;
269272
});
270273
event.dispatcher.on(event.test.skipped, () => {

lib/output.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ module.exports = {
206206
* @param {number} skipped
207207
* @param {number|string} duration
208208
*/
209-
result(passed, failed, skipped, duration) {
209+
result(passed, failed, skipped, duration, failedHooks = 0) {
210210
let style = colors.bgGreen;
211211
let msg = ` ${passed || 0} passed`;
212212
let status = style.bold(' OK ');
@@ -215,6 +215,12 @@ module.exports = {
215215
status = style.bold(' FAIL ');
216216
msg += `, ${failed} failed`;
217217
}
218+
219+
if (failedHooks > 0) {
220+
style = style.bgRed;
221+
status = style.bold(' FAIL ');
222+
msg += `, ${failedHooks} failedHooks`;
223+
}
218224
status += style.grey(' |');
219225

220226
if (skipped) {

lib/workers.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ class Workers extends EventEmitter {
357357

358358
run() {
359359
this.stats.start = new Date();
360+
this.stats.failedHooks = 0
360361
recorder.startUnlessRunning();
361362
event.dispatcher.emit(event.workers.before);
362363
process.env.RUNS_WITH_WORKERS = 'true';
@@ -471,6 +472,7 @@ class Workers extends EventEmitter {
471472
this.stats.failures += newStats.failures;
472473
this.stats.tests += newStats.tests;
473474
this.stats.pending += newStats.pending;
475+
this.stats.failedHooks += newStats.failedHooks;
474476
}
475477

476478
printResults() {
@@ -492,7 +494,7 @@ class Workers extends EventEmitter {
492494
this.failuresLog.forEach(log => output.print(...log));
493495
}
494496

495-
output.result(this.stats.passes, this.stats.failures, this.stats.pending, ms(this.stats.duration));
497+
output.result(this.stats.passes, this.stats.failures, this.stats.pending, ms(this.stats.duration), this.stats.failedHooks);
496498
process.env.RUNS_WITH_WORKERS = 'false';
497499
}
498500
}

test/runner/before_failure_test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ describe('Failure in before', function () {
99
this.timeout(40000)
1010
it('should skip tests that are skipped because of failure in before hook', (done) => {
1111
exec(`${codecept_run}`, (err, stdout) => {
12-
stdout.should.include('First test will be passed')
13-
stdout.should.include('S Third test will be skipped @grep')
14-
stdout.should.include('S Fourth test will be skipped')
15-
stdout.should.include('1 passed, 1 failed, 2 skipped')
12+
stdout.should.include('First test will be passed @grep')
13+
stdout.should.include('Third test will be skipped @grep')
14+
stdout.should.include('Fourth test will be skipped')
15+
stdout.should.include('1 passed, 1 failedHooks, 2 skipped')
1616
err.code.should.eql(1)
1717
done()
1818
})
1919
})
2020

2121
it('should skip tests correctly with grep options', (done) => {
2222
exec(`${codecept_run} --grep @grep`, (err, stdout) => {
23-
stdout.should.include('First test will be passed')
24-
stdout.should.include('S Third test will be skipped @grep')
25-
stdout.should.include('1 passed, 1 failed, 1 skipped')
23+
stdout.should.include('First test will be passed @grep')
24+
stdout.should.include('Third test will be skipped @grep')
25+
stdout.should.include('1 passed, 1 failedHooks, 1 skipped')
2626
err.code.should.eql(1)
2727
done()
2828
})

test/runner/run_workers_test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ describe('CodeceptJS Workers Runner', function () {
4040
expect(stdout).toContain('glob current dir')
4141
expect(stdout).toContain('From worker @1_grep print message 1')
4242
expect(stdout).toContain('From worker @2_grep print message 2')
43-
expect(stdout).toContain('Running tests in 3 workers')
43+
expect(stdout).toContain('Running tests in')
4444
expect(stdout).not.toContain('this is running inside worker')
4545
expect(stdout).toContain('failed')
4646
expect(stdout).toContain('File notafile not found')
47-
expect(stdout).toContain('Scenario Steps:')
48-
expect(stdout).toContain('FAIL | 5 passed, 2 failed')
47+
expect(stdout).toContain('5 passed, 1 failed, 1 failedHooks')
4948
// We are not testing order in logs, because it depends on race condition between workers
5049
expect(stdout).toContain(') Workers Failing\n') // first fail log
5150
expect(stdout).toContain(') Workers\n') // second fail log

0 commit comments

Comments
 (0)