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 5, 2023
1 parent a6d0b31 commit 1e25880
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
test/fixtures
8 changes: 7 additions & 1 deletion src/cmd/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ export class TestCommand extends BaseCommand {

const mochaArgs = await this.formatMochaArgs();
if (!mochaArgs) return;
await this.forkNode(mochaFile, mochaArgs);
await this.forkNode(mochaFile, mochaArgs, {
execArgv: [
...process.execArgv,
// https://github.com/mochajs/mocha/issues/2640#issuecomment-1663388547
'--unhandled-rejections=strict',
],
});
}

protected async formatMochaArgs() {
Expand Down
8 changes: 8 additions & 0 deletions test/cmd/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ describe('test/cmd/test.test.ts', () => {
.end();
});

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

it('test parallel', () => {
if (process.platform === 'win32') return;
return coffee.fork(eggBin, [ 'test', '--parallel' ], {
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"
]
}
5 changes: 5 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,5 @@
describe('a.test.js', () => {
it('should success', () => {
Promise.reject(new Error('mock error'));
});
});

1 comment on commit 1e25880

@vercel
Copy link

@vercel vercel bot commented on 1e25880 Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

egg-bin – ./

egg-bin.vercel.app
egg-bin-git-master-no-veronica.vercel.app
egg-bin-no-veronica.vercel.app

Please sign in to comment.