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 11dcfee commit d7e522b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ test/fixtures/example-ts-ets/typings/
**/run/*.json
.tmp
.vscode
.idea
.cache
*.log
package-lock.json
.nyc_output
yarn.lock
yarn.lock
6 changes: 5 additions & 1 deletion lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,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',
],
};
const mochaFile = require.resolve('mocha/bin/_mocha');
const testArgs = yield this.formatTestArgs(context);
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', () => {

Check failure on line 1 in test/fixtures/test-unhandled-rejection/test/a.test.js

View workflow job for this annotation

GitHub Actions / build (14, ubuntu-latest)

Use the global form of 'use strict'

Check failure on line 1 in test/fixtures/test-unhandled-rejection/test/a.test.js

View workflow job for this annotation

GitHub Actions / build (14, windows-latest)

Use the global form of 'use strict'

Check failure on line 1 in test/fixtures/test-unhandled-rejection/test/a.test.js

View workflow job for this annotation

GitHub Actions / build (14, macos-latest)

Use the global form of 'use strict'

Check failure on line 1 in test/fixtures/test-unhandled-rejection/test/a.test.js

View workflow job for this annotation

GitHub Actions / build (16, ubuntu-latest)

Use the global form of 'use strict'

Check failure on line 1 in test/fixtures/test-unhandled-rejection/test/a.test.js

View workflow job for this annotation

GitHub Actions / build (16, windows-latest)

Use the global form of 'use strict'

Check failure on line 1 in test/fixtures/test-unhandled-rejection/test/a.test.js

View workflow job for this annotation

GitHub Actions / build (16, macos-latest)

Use the global form of 'use strict'
it('should success', () => {
Promise.reject(new Error('mock error'));
});
});
8 changes: 8 additions & 0 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,12 @@ describe('test/lib/cmd/test.test.js', () => {
.end(done);
});
});

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 d7e522b

Please sign in to comment.