From 4f4bfb34762ea211ced4f73214167c56d1af1714 Mon Sep 17 00:00:00 2001 From: XYShaoKang Date: Sat, 4 Sep 2021 10:55:46 +0800 Subject: [PATCH] chore(video-http-server): fix jest reporter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复因 jest 使用 stderr 输出警告而导致的 CI 执行失败 https://github.com/microsoft/rushstack/issues/1329 --- apps/video-http-server/jest.config.js | 3 +- apps/video-http-server/jest/JestReporter.js | 30 +++++++++++++++++++ .../{ => jest}/setupTests.ts | 0 apps/video-http-server/tsconfig.build.json | 1 + apps/video-http-server/tsconfig.json | 2 +- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 apps/video-http-server/jest/JestReporter.js rename apps/video-http-server/{ => jest}/setupTests.ts (100%) diff --git a/apps/video-http-server/jest.config.js b/apps/video-http-server/jest.config.js index 9c074a7..622a84d 100644 --- a/apps/video-http-server/jest.config.js +++ b/apps/video-http-server/jest.config.js @@ -1,7 +1,8 @@ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest', - setupFilesAfterEnv: ['/setupTests.ts'], + setupFilesAfterEnv: ['/jest/setupTests.ts'], + reporters: ['./jest/JestReporter.js'], testEnvironment: 'node', testMatch: ['**/?(*.)+(spec|test).[t]s?(x)'], } diff --git a/apps/video-http-server/jest/JestReporter.js b/apps/video-http-server/jest/JestReporter.js new file mode 100644 index 0000000..4bbeb76 --- /dev/null +++ b/apps/video-http-server/jest/JestReporter.js @@ -0,0 +1,30 @@ +// source: https://github.com/microsoft/just/blob/47ecde0d578b223a8abf26eebd0eff02ff331988/packages/just-scripts/src/jest/JestReporter.ts + +// This doesn't have types for some reason +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { DefaultReporter } = require('@jest/reporters') + +/** + * The purpose of this custom reporter is to prevent Jest from logging to stderr + * when there are no errors. + */ +class JestReporter extends DefaultReporter { + _isLoggingError = false + + log(message) { + if (this._isLoggingError) { + process.stderr.write(message + '\n') + } else { + process.stdout.write(message + '\n') + } + } + + printTestFileFailureMessage(...args) { + this._isLoggingError = true + super.printTestFileFailureMessage(...args) + this._isLoggingError = false + } +} + +// jest needs this format +module.exports = JestReporter diff --git a/apps/video-http-server/setupTests.ts b/apps/video-http-server/jest/setupTests.ts similarity index 100% rename from apps/video-http-server/setupTests.ts rename to apps/video-http-server/jest/setupTests.ts diff --git a/apps/video-http-server/tsconfig.build.json b/apps/video-http-server/tsconfig.build.json index 99c5f99..6ab6d38 100644 --- a/apps/video-http-server/tsconfig.build.json +++ b/apps/video-http-server/tsconfig.build.json @@ -1,4 +1,5 @@ { "extends": "./tsconfig", + "include": ["src/**/*"], "exclude": ["**/*.test.ts"] } diff --git a/apps/video-http-server/tsconfig.json b/apps/video-http-server/tsconfig.json index 9c707d3..186d710 100644 --- a/apps/video-http-server/tsconfig.json +++ b/apps/video-http-server/tsconfig.json @@ -17,5 +17,5 @@ "sourceMap": true, "target": "ES2020" }, - "include": ["src/**/*"] + "include": ["src/**/*", "jest-config/**/*"] }