Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): print failed hooks #4476

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Cli extends Base {

result() {
const stats = this.stats;
stats.failedHooks = 0;
console.log();

// passes
Expand Down Expand Up @@ -216,8 +217,14 @@ class Cli extends Base {
console.log();
}

this.failures.forEach((failure) => {
if (failure.constructor.name === 'Hook') {
stats.failures -= stats.failures
stats.failedHooks += 1
}
})
event.emit(event.all.failures, { failuresLog, stats });
output.result(stats.passes, stats.failures, stats.pending, ms(stats.duration));
output.result(stats.passes, stats.failures, stats.pending, ms(stats.duration), stats.failedHooks);

if (stats.failures && output.level() < 3) {
output.print(output.styles.debug('Run with --verbose flag to see complete NodeJS stacktrace'));
Expand Down
5 changes: 4 additions & 1 deletion lib/command/workers/runTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ function collectStats() {
event.dispatcher.on(event.test.passed, () => {
stats.passes++;
});
event.dispatcher.on(event.test.failed, () => {
event.dispatcher.on(event.test.failed, (test) => {
if (test.ctx._runnable.title.includes('hook: AfterSuite')) {
stats.failedHooks += 1;
}
stats.failures++;
});
event.dispatcher.on(event.test.skipped, () => {
Expand Down
8 changes: 7 additions & 1 deletion lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ module.exports = {
* @param {number} skipped
* @param {number|string} duration
*/
result(passed, failed, skipped, duration) {
result(passed, failed, skipped, duration, failedHooks = 0) {
let style = colors.bgGreen;
let msg = ` ${passed || 0} passed`;
let status = style.bold(' OK ');
Expand All @@ -215,6 +215,12 @@ module.exports = {
status = style.bold(' FAIL ');
msg += `, ${failed} failed`;
}

if (failedHooks > 0) {
style = style.bgRed;
status = style.bold(' FAIL ');
msg += `, ${failedHooks} failedHooks`;
}
status += style.grey(' |');

if (skipped) {
Expand Down
4 changes: 3 additions & 1 deletion lib/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class Workers extends EventEmitter {

run() {
this.stats.start = new Date();
this.stats.failedHooks = 0
recorder.startUnlessRunning();
event.dispatcher.emit(event.workers.before);
process.env.RUNS_WITH_WORKERS = 'true';
Expand Down Expand Up @@ -471,6 +472,7 @@ class Workers extends EventEmitter {
this.stats.failures += newStats.failures;
this.stats.tests += newStats.tests;
this.stats.pending += newStats.pending;
this.stats.failedHooks += newStats.failedHooks;
}

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

output.result(this.stats.passes, this.stats.failures, this.stats.pending, ms(this.stats.duration));
output.result(this.stats.passes, this.stats.failures, this.stats.pending, ms(this.stats.duration), this.stats.failedHooks);
process.env.RUNS_WITH_WORKERS = 'false';
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/runner/before_failure_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ describe('Failure in before', function () {
this.timeout(40000)
it('should skip tests that are skipped because of failure in before hook', (done) => {
exec(`${codecept_run}`, (err, stdout) => {
stdout.should.include('First test will be passed')
stdout.should.include('S Third test will be skipped @grep')
stdout.should.include('S Fourth test will be skipped')
stdout.should.include('1 passed, 1 failed, 2 skipped')
stdout.should.include('First test will be passed @grep')
stdout.should.include('Third test will be skipped @grep')
stdout.should.include('Fourth test will be skipped')
stdout.should.include('1 passed, 1 failedHooks, 2 skipped')
err.code.should.eql(1)
done()
})
})

it('should skip tests correctly with grep options', (done) => {
exec(`${codecept_run} --grep @grep`, (err, stdout) => {
stdout.should.include('First test will be passed')
stdout.should.include('S Third test will be skipped @grep')
stdout.should.include('1 passed, 1 failed, 1 skipped')
stdout.should.include('First test will be passed @grep')
stdout.should.include('Third test will be skipped @grep')
stdout.should.include('1 passed, 1 failedHooks, 1 skipped')
err.code.should.eql(1)
done()
})
Expand Down
5 changes: 2 additions & 3 deletions test/runner/run_workers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ describe('CodeceptJS Workers Runner', function () {
expect(stdout).toContain('glob current dir')
expect(stdout).toContain('From worker @1_grep print message 1')
expect(stdout).toContain('From worker @2_grep print message 2')
expect(stdout).toContain('Running tests in 3 workers')
expect(stdout).toContain('Running tests in')
expect(stdout).not.toContain('this is running inside worker')
expect(stdout).toContain('failed')
expect(stdout).toContain('File notafile not found')
expect(stdout).toContain('Scenario Steps:')
expect(stdout).toContain('FAIL | 5 passed, 2 failed')
expect(stdout).toContain('5 passed, 1 failed, 1 failedHooks')
// We are not testing order in logs, because it depends on race condition between workers
expect(stdout).toContain(') Workers Failing\n') // first fail log
expect(stdout).toContain(') Workers\n') // second fail log
Expand Down
Loading