Skip to content

Commit

Permalink
Merge pull request microsoft#4402 from microsoft/octogonz/jest-consol…
Browse files Browse the repository at this point in the history
…e-log

[heft] "heft --debug test" and "heft test --verbose" should enable Jest's verbose mode
  • Loading branch information
octogonz authored Oct 21, 2023
2 parents ac85843 + 0aa855b commit 8d6ba52
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "@rushstack/heft-jest-plugin/includes/jest-shared.config.json",
"coverageDirectory": "<rootDir>/coverage",
"reporters": ["default", "../lib/test/customJestReporter.cjs"],
"testMatch": ["<rootDir>/lib/**.test.cjs"],
"testMatch": ["<rootDir>/lib/**/*.test.cjs"],
"collectCoverageFrom": [
"lib/**/*.cjs",
"!lib/**/*.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-jest-plugin",
"comment": "Use Jest verbose logging when `heft --debug test` or `heft test --verbose` is specified",
"type": "minor"
}
],
"packageName": "@rushstack/heft-jest-plugin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-jest-plugin",
"comment": "Fix an issue where `silent: true` was ignored when specified in `jest.config.json`",
"type": "minor"
}
],
"packageName": "@rushstack/heft-jest-plugin"
}
30 changes: 29 additions & 1 deletion heft-plugins/heft-jest-plugin/src/JestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ export default class JestPlugin implements IHeftTaskPlugin<IJestPluginOptions> {
jestConfig.displayName = heftConfiguration.projectPackageJson.name;
}

let silent: boolean | undefined;
if (taskSession.parameters.verbose || taskSession.parameters.debug) {
// If Heft's "--verbose" or "--debug" parameters were used, then we're debugging Jest problems,
// so we always want to see "console.log()" even if jest.config.json asked to suppress it.
// If someone really dislikes that, we could expose "--silent" in the Heft CLI,
// but it is a confusing combination.
silent = false;
} else {
// If "silent" is specified via IJestPluginOptions, that takes precedence over jest.config.json
silent = options.silent ?? jestConfig.silent ?? false;
}

const jestArgv: Config.Argv = {
// In debug mode, avoid forking separate processes that are difficult to debug
runInBand: taskSession.parameters.debug,
Expand All @@ -590,7 +602,23 @@ export default class JestPlugin implements IHeftTaskPlugin<IJestPluginOptions> {
listTests: false,
rootDir: buildFolderPath,

silent: options.silent || false,
// What these fields mean for Jest:
//
// If "silent" is true:
// - Jest discards all console.log() output and there is no way to retrieve it
//
// If "silent" is false and "verbose" is false:
// - Jest uses BufferedConsole which doesn't show console.log() until after the test run completes,
// which is annoying in the debugger. The output is formatted nicely using HeftJestReporter.
//
// If "silent" is false and "verbose" is true:
// - Jest uses CustomConsole which logs immediately, but shows ugly call stacks with each log.
//
// If "verbose" is true (regardless of "silent"):
// - Jest reports include detailed results for every test, even if all tests passed within a test suite.
silent,
verbose: taskSession.parameters.verbose || taskSession.parameters.debug,

testNamePattern: options.testNamePattern,
testPathIgnorePatterns: options.testPathIgnorePatterns ? [options.testPathIgnorePatterns] : undefined,
testPathPattern: options.testPathPattern ? [options.testPathPattern] : undefined,
Expand Down

0 comments on commit 8d6ba52

Please sign in to comment.