Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8999b7b
Option to use posix exit code upon fatal signal
73rhodes May 23, 2023
3b99517
Update test/integration/options/posixExitCodes.spec.js
73rhodes Mar 15, 2024
2150c30
Address PR review comments mocha org
73rhodes Mar 18, 2024
0fb6ca3
Merge pull request #2 from zendesk/dderidder/X-mochajs-pr-review
73rhodes Mar 18, 2024
62528c5
Address PR review comments mocha org
73rhodes Mar 18, 2024
8a4f2bd
Merge pull request #3 from zendesk/dderidder/X-mochajs-pr-review
73rhodes Mar 18, 2024
817d890
Merge branch 'master' into master
73rhodes Mar 18, 2024
ad30057
Update docs/index.md
73rhodes Mar 25, 2024
592f071
Fix exit code when no signal caught
73rhodes Apr 11, 2024
34f2822
Coverage for posix exit code with normally failing tests
73rhodes Apr 11, 2024
3438da6
Merge pull request #4 from zendesk/dderidder/X-mochajs-pr-review-part2
73rhodes Apr 11, 2024
b6e6d30
Update docs/index.md
73rhodes Apr 15, 2024
d2991a1
Address PR comments; use os.constants
73rhodes Apr 15, 2024
7cf79d8
Merge pull request #5 from zendesk/dderidder/X-mochajs-pr-review-part3
73rhodes Apr 15, 2024
609f94f
Remove test that asserts the problematic behavior
73rhodes Apr 23, 2024
b71eaf3
Merge pull request #6 from zendesk/dderidder/X-mochajs-pr-review-part4
73rhodes Apr 23, 2024
26bebac
Merge branch 'mochajs:master' into master
73rhodes Apr 24, 2024
0aa939f
Omit win32 from signals test suite
73rhodes Apr 26, 2024
59e471d
Merge pull request #7 from zendesk/dderidder/X-win32-fixup
73rhodes Apr 26, 2024
6d237fc
Update docs/index.md
73rhodes May 1, 2024
ca78dbc
Update package.json
73rhodes Jun 7, 2024
1809576
Merge pull request #8 from zendesk/73rhodes-patch-1
73rhodes Jun 7, 2024
5cad7db
build(deps): bump the npm_and_yarn group across 1 directory with 13 u…
dependabot[bot] Jun 18, 2024
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
Prev Previous commit
Next Next commit
Address PR comments; use os.constants
  • Loading branch information
73rhodes committed Apr 15, 2024
commit d2991a18c1b02c7f23e8abc62631b06d6dfa3b3a
9 changes: 7 additions & 2 deletions bin/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const {aliases} = require('../lib/cli/run-option-metadata');

const mochaArgs = {};
const nodeArgs = {};
const EXIT_SUCCESS = 0;
const EXIT_FAILURE = 1;
const SIGNAL_OFFSET= 128;
let hasInspect = false;

const opts = loadOptions(process.argv.slice(2));
Expand Down Expand Up @@ -113,12 +116,14 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) {
const numericSignal =
typeof signal === 'string' ? os.constants.signals[signal] : signal;
if (mochaArgs['posix-exit-codes'] === true) {
process.exit(128 + numericSignal);
process.exit(SIGNAL_OFFSET + numericSignal);
} else {
process.kill(process.pid, signal);
}
} else if (code !== 0 && mochaArgs['posix-exit-codes'] === true) {
process.exit(EXIT_FAILURE);
} else {
process.exit((mochaArgs['posix-exit-codes'] === true) ? 0 : code);
process.exit(code);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ exports.builder = yargs =>
group: GROUPS.RULES
},
'posix-exit-codes': {
description: 'Use posix exit codes for fatal signals',
description: 'Use POSIX and UNIX shell exit codes as Mocha\'s return value',
group: GROUPS.RULES
},
recursive: {
Expand Down
23 changes: 23 additions & 0 deletions test/integration/fixtures/failing.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

// One passing test and three failing tests

var assert = require('assert');

describe('suite', function () {
it('test1', function () {
assert(true);
});

it('test2', function () {
assert(false);
});

it('test3', function () {
assert(false);
});

it('test4', function () {
assert(false);
});
});
22 changes: 14 additions & 8 deletions test/integration/options/posixExitCodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,45 @@

var helpers = require('../helpers');
var runMocha = helpers.runMocha;
var os = require('os');

const EXIT_SUCCESS = 0;
const EXIT_FAILURE = 1;
const SIGNAL_OFFSET = 128;

describe('--posix-exit-codes', 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) {
it('should exit with correct POSIX shell code on SIGABRT', function (done) {
var fixture = 'signals-sigabrt.fixture.js';
runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 134);
expect(res.code, 'to be', SIGNAL_OFFSET + os.constants.signals.SIGABRT);
done();
});
});

it('should exit with code 143 on SIGTERM', function (done) {
it('should exit with correct POSIX shell code on SIGTERM', function (done) {
var fixture = 'signals-sigterm.fixture.js';
runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 143);
expect(res.code, 'to be', SIGNAL_OFFSET + os.constants.signals.SIGTERM);
done();
});
});

it('should exit with code 0 even if there are test failures', function (done) {
it('should exit with code 1 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);
expect(res.code, 'to be', EXIT_FAILURE);
done();
});
});
Expand All @@ -55,13 +60,14 @@ describe('--posix-exit-codes', function () {
});

it('should exit with the number of failed tests', function (done) {
var fixture = 'failing.fixture.js'; // one failing test
var fixture = 'failing.fixture.js'; // contains three failing tests
var numFailures = 3;
var args = ['--no-warnings'];
runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 1);
expect(res.code, 'to be', numFailures);
done();
});
});
Expand Down