-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce that tests don't log (#5227)
When `npm test` prints out a million "starting usage reporting" logs, it gets in the way of finding failing tests. This change stops tests from logging and makes them fail if they do log. (You can run with ALLOW_CONSOLE=t locally to disable this behavior.)
- Loading branch information
Showing
6 changed files
with
59 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// It's awkward to read `npm test` output if it is littered with console logs. | ||
// This (from https://github.com/facebook/jest/issues/6121) makes the test fail | ||
// if it writes anything to console. (Feel free to set $ALLOW_CONSOLE if you are | ||
// logging while developing and want to see if your tests pass.) | ||
// | ||
// Typically you can turn off the info/debug in tests by passing a loglevel | ||
// logger with level WARN to the logger option to `new ApolloServer`. | ||
if (!process.env.ALLOW_CONSOLE) { | ||
let usedConsole = false; | ||
['log', 'error', 'warn', 'info', 'debug'].forEach((key) => { | ||
const originalFn = console[key]; | ||
console[key] = (...args) => { | ||
usedConsole = true; | ||
originalFn(...args); | ||
}; | ||
}); | ||
|
||
jasmine.getEnv().addReporter({ | ||
specStarted: (result) => (jasmine.currentTest = result), | ||
}); | ||
|
||
afterEach(() => { | ||
if (usedConsole && !jasmine.currentTest.failedExpectations.length) { | ||
usedConsole = false; | ||
throw Error('To keep unit test output readable, tests should not write to the console. To test logging behavior, pass a logger to the class under test.'); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters