From 7f001a923948901bf0b41a83e5950d69cf42fee3 Mon Sep 17 00:00:00 2001 From: killagu Date: Thu, 3 Aug 2023 15:31:50 +0800 Subject: [PATCH 1/2] fix: convert unhandled rejection to uncaught exception Mocha do not catch unhandled rejection by default. Case will faield until timeout. set `--unhandled-rejections` to strict, let case fail fast. --- src/cmd/test.ts | 8 +++++++- test/cmd/test.test.ts | 8 ++++++++ test/fixtures/test-unhandled-rejection/package.json | 6 ++++++ test/fixtures/test-unhandled-rejection/test/a.test.js | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) 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/src/cmd/test.ts b/src/cmd/test.ts index b47216bd..40eb5ec4 100644 --- a/src/cmd/test.ts +++ b/src/cmd/test.ts @@ -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() { diff --git a/test/cmd/test.test.ts b/test/cmd/test.test.ts index 767a28d5..b4626aef 100644 --- a/test/cmd/test.test.ts +++ b/test/cmd/test.test.ts @@ -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' ], { 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')); + }); +}); From ebca170af2a8c55be158dbed5bef4dad647918c3 Mon Sep 17 00:00:00 2001 From: killagu Date: Sat, 5 Aug 2023 12:05:48 +0800 Subject: [PATCH 2/2] ci: eslint ignore test/fixtures --- .eslintignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintignore b/.eslintignore index 25fbf5a1..d6699189 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ node_modules/ coverage/ +test/fixtures