Skip to content

Commit

Permalink
fix: convert unhandled rejection to uncaught exception (#235) (#236)
Browse files Browse the repository at this point in the history
* fix: convert unhandled rejection to uncaught exception (#235)

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 authored Aug 7, 2023
1 parent c4301c5 commit e98e079
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
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'));
});
});
9 changes: 8 additions & 1 deletion test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,14 @@ describe('test/lib/cmd/test.test.js', () => {
// .debug()
.expect('stdout', /env\.AUTO_AGENT: true/)
.expect('stdout', /env\.ENABLE_MOCHA_PARALLEL: true/)
.expect('code', 0)
.expect('code', 0);
});

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 e98e079

Please sign in to comment.