Skip to content

Commit

Permalink
chore(front-end): fix jest reporter
Browse files Browse the repository at this point in the history
修复因 jest 使用 stderr 输出警告而导致的 CI 执行失败
microsoft/rushstack#1329
  • Loading branch information
XYShaoKang committed Sep 8, 2021
1 parent 64ab4d4 commit fcaa34e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/front-end/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const config = {
verbose: true,
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
reporters: ['./src/JestReporter.js'],
testMatch: ['**/src/**/*.test.ts?(x)'],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
Expand Down
28 changes: 28 additions & 0 deletions apps/front-end/src/JestReporter.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fcaa34e

Please sign in to comment.