Skip to content

Commit

Permalink
fix: convert unhandled rejection to uncaught exception (#235)
Browse files Browse the repository at this point in the history
Mocha do not catch unhandled rejection by default. Case
will faield until timeout. set `--unhandled-rejections`
to strict, let case fail fast.
  • Loading branch information
killagu committed Aug 7, 2023
1 parent c4301c5 commit 489afd8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ class TestCommand extends Command {
env: Object.assign({
NODE_ENV: 'test',
}, context.env),
execArgv: context.execArgv,
execArgv: [
...context.execArgv,
// https://github.com/mochajs/mocha/issues/2640#issuecomment-1663388547
'--unhandled-rejections=strict',
],
};
await this.helper.forkNode(mochaFile, testArgs, opt);
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/test-unhandled-rejection/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-unhandled-rejection",
"files": [
"lib"
]
}
7 changes: 7 additions & 0 deletions test/fixtures/test-unhandled-rejection/test/a.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('a.test.js', () => {
it('should success', () => {
Promise.reject(new Error('mock error'));
});
});
7 changes: 7 additions & 0 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ describe('test/lib/cmd/test.test.js', () => {
.expect('stdout', /env\.AUTO_AGENT: true/)
.expect('stdout', /env\.ENABLE_MOCHA_PARALLEL: true/)
.expect('code', 0)

Check failure on line 293 in test/lib/cmd/test.test.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

Missing semicolon

Check failure on line 293 in test/lib/cmd/test.test.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 16)

Missing semicolon

Check failure on line 293 in test/lib/cmd/test.test.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 14)

Missing semicolon

Check failure on line 293 in test/lib/cmd/test.test.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

Missing semicolon

Check failure on line 293 in test/lib/cmd/test.test.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

Missing semicolon

Check failure on line 293 in test/lib/cmd/test.test.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

Missing semicolon
});

it('should failed with unhandled rejection', () => {
return coffee.fork(eggBin, [ 'test' ], { cwd: path.join(__dirname, '../../fixtures/test-unhandled-rejection') })
.debug()
.expect('stdout', / Uncaught Error: mock error/)
.expect('code', 1)
.end();
});
});

0 comments on commit 489afd8

Please sign in to comment.