From d7e522b73f9d326a2985f30df564aa3e15dd2b63 Mon Sep 17 00:00:00 2001 From: killa Date: Sat, 5 Aug 2023 15:18:19 +0800 Subject: [PATCH] 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. --- .gitignore | 3 ++- lib/cmd/test.js | 6 +++++- test/fixtures/test-unhandled-rejection/package.json | 6 ++++++ test/fixtures/test-unhandled-rejection/test/a.test.js | 5 +++++ test/lib/cmd/test.test.js | 8 ++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/test-unhandled-rejection/package.json create mode 100644 test/fixtures/test-unhandled-rejection/test/a.test.js diff --git a/.gitignore b/.gitignore index b3c359bf..74b1ea44 100644 --- a/.gitignore +++ b/.gitignore @@ -22,8 +22,9 @@ test/fixtures/example-ts-ets/typings/ **/run/*.json .tmp .vscode +.idea .cache *.log package-lock.json .nyc_output -yarn.lock \ No newline at end of file +yarn.lock diff --git a/lib/cmd/test.js b/lib/cmd/test.js index d581c48c..02abb610 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -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); diff --git a/test/fixtures/test-unhandled-rejection/package.json b/test/fixtures/test-unhandled-rejection/package.json new file mode 100644 index 00000000..1633ac83 --- /dev/null +++ b/test/fixtures/test-unhandled-rejection/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-unhandled-rejection", + "files": [ + "lib" + ] +} diff --git a/test/fixtures/test-unhandled-rejection/test/a.test.js b/test/fixtures/test-unhandled-rejection/test/a.test.js new file mode 100644 index 00000000..61cdb364 --- /dev/null +++ b/test/fixtures/test-unhandled-rejection/test/a.test.js @@ -0,0 +1,5 @@ +describe('a.test.js', () => { + it('should success', () => { + Promise.reject(new Error('mock error')); + }); +}); diff --git a/test/lib/cmd/test.test.js b/test/lib/cmd/test.test.js index 0e8e0dfd..91e1419f 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -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(); + }); });