From 592f071845f54407172e1a1a59df84278f2d8a18 Mon Sep 17 00:00:00 2001 From: Darren DeRidder Date: Thu, 11 Apr 2024 15:19:57 -0400 Subject: [PATCH 1/2] Fix exit code when no signal caught --- bin/mocha.js | 2 +- test/integration/options/posixExitCodes.spec.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bin/mocha.js b/bin/mocha.js index dee9d3a67d..d5f914a462 100755 --- a/bin/mocha.js +++ b/bin/mocha.js @@ -118,7 +118,7 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) { process.kill(process.pid, signal); } } else { - process.exit(code); + process.exit((mochaArgs['posix-exit-codes'] === true) ? 0 : code); } }); }); diff --git a/test/integration/options/posixExitCodes.spec.js b/test/integration/options/posixExitCodes.spec.js index f33f65a0ff..ea16462e87 100644 --- a/test/integration/options/posixExitCodes.spec.js +++ b/test/integration/options/posixExitCodes.spec.js @@ -42,5 +42,17 @@ describe('--posix-exit-codes', function () { done(); }); }); + + it('should exit with the number of failed tests', function (done) { + var fixture = 'failing.fixture.js'; // one failing test + var args = ['--no-warnings']; + runMocha(fixture, args, function postmortem(err, res) { + if (err) { + return done(err); + } + expect(res.code, 'to be', 1); + done(); + }); + }); }); }); From 34f282212aee6d808c480169e6368294a2bc03de Mon Sep 17 00:00:00 2001 From: Darren DeRidder Date: Thu, 11 Apr 2024 15:45:57 -0400 Subject: [PATCH 2/2] Coverage for posix exit code with normally failing tests --- test/integration/options/posixExitCodes.spec.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/integration/options/posixExitCodes.spec.js b/test/integration/options/posixExitCodes.spec.js index ea16462e87..4510a7c42c 100644 --- a/test/integration/options/posixExitCodes.spec.js +++ b/test/integration/options/posixExitCodes.spec.js @@ -4,7 +4,7 @@ var helpers = require('../helpers'); var runMocha = helpers.runMocha; describe('--posix-exit-codes', function () { - describe('when enabled with node options', function () { + describe('when enabled, and with node options', function () { var args = ['--no-warnings', '--posix-exit-codes']; it('should exit with code 134 on SIGABRT', function (done) { @@ -28,9 +28,20 @@ describe('--posix-exit-codes', function () { done(); }); }); + + it('should exit with code 0 even if there are test failures', function (done) { + var fixture = 'failing.fixture.js'; + runMocha(fixture, args, function postmortem(err, res) { + if (err) { + return done(err); + } + expect(res.code, 'to be', 0); + done(); + }); + }); }); - describe('when not enabled with node options', function () { + describe('when not enabled, and with node options', function () { it('should exit with code null on SIGABRT', function (done) { var fixture = 'signals-sigabrt.fixture.js'; var args = ['--no-warnings'];