From fcaa34edf8f915f7ab7a5020212277cadda9cc22 Mon Sep 17 00:00:00 2001 From: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com> Date: Sat, 4 Sep 2021 10:56:38 +0800 Subject: [PATCH] chore(front-end): 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/front-end/jest.config.js | 1 + apps/front-end/src/JestReporter.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 apps/front-end/src/JestReporter.js diff --git a/apps/front-end/jest.config.js b/apps/front-end/jest.config.js index f127687..3a8c1a6 100644 --- a/apps/front-end/jest.config.js +++ b/apps/front-end/jest.config.js @@ -2,6 +2,7 @@ const config = { verbose: true, setupFilesAfterEnv: ['/src/setupTests.ts'], + reporters: ['./src/JestReporter.js'], testMatch: ['**/src/**/*.test.ts?(x)'], transform: { '^.+\\.[jt]sx?$': 'babel-jest', diff --git a/apps/front-end/src/JestReporter.js b/apps/front-end/src/JestReporter.js new file mode 100644 index 0000000..ede71b3 --- /dev/null +++ b/apps/front-end/src/JestReporter.js @@ -0,0 +1,28 @@ +// 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