Этого можно добиться, передав пользовательский TestEnvironment и пользовательский Reporter (протестированный с помощью jest@24.7.1):
// debugTestEnv.js
const NodeEnvironment = require('jest-environment-node')
const console = require('console')
const vanillaConsole = new console.Console({ stdout: process.stdout, stderr: process.stderr })
class DebugTestEnv extends NodeEnvironment {
async setup() {
await super.setup()
this.prevConsole = this.global.console
this.global.console = vanillaConsole
}
async teardown() {
this.global.console = this.prevConsole
await super.teardown()
}
}
module.exports = DebugTestEnv
// debugReporter.js
// In Jest, the DefaultEnvironment constructor changes `process.stdout` and `process.stderr`
// That's why we pass a custom reporter to Jest (we use Jest's BaseReporter for this purpose)
module.exports = require('@jest/reporters/build/base_reporter').default
затем для запуска определенного теста:
jest --runInBand --no-cache --watchAll=false -t="testNameToDebug" --env="./path/to/debugTestEnv.js" --reporters="./path/to/debugReporter.js"
Это также работает с приложением create-реагировать, просто замените jest
на react-scripts test
.