diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 7c2d3ea1..644c2390 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -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); } 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..e10d4e69 --- /dev/null +++ b/test/fixtures/test-unhandled-rejection/test/a.test.js @@ -0,0 +1,7 @@ +'use strict'; + +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 69648ce1..8995006d 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -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(); }); });