Skip to content

Commit 3b9b2a4

Browse files
SimonMuellerSimon Müller
andauthored
add configuration flag to immediately print from original console method (#49)
Co-authored-by: Simon Müller <[email protected]>
1 parent fe08d00 commit 3b9b2a4

File tree

12 files changed

+72
-2
lines changed

12 files changed

+72
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ failOnConsole({
194194
})
195195
```
196196

197+
### shouldPrintMessage
198+
199+
Use this to print the message immediately when called not awaiting the test to finish. This is useful to show the message if there are
200+
other or earlier test failures which will result in the fail on console error to be hidden by jest.
201+
202+
- Type: `boolean`
203+
- Default: `false`
204+
197205
## License
198206

199207
[MIT](https://github.com/ValentinH/jest-fail-on-console/blob/master/LICENSE)

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ declare namespace init {
5252
methodName: ConsoleMethodName,
5353
context: { group: string; groups: string[] }
5454
) => boolean
55+
56+
/** @default false */
57+
shouldPrintMessage?: boolean
5558
}
5659
}
5760

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const init = ({
2424
skipTest,
2525
silenceMessage,
2626
allowMessage,
27+
shouldPrintMessage = false,
2728
} = {}) => {
2829
const flushUnexpectedConsoleCalls = (methodName, unexpectedConsoleCallStacks) => {
2930
if (unexpectedConsoleCallStacks.length > 0) {
@@ -72,6 +73,10 @@ const init = ({
7273
return
7374
}
7475

76+
if (shouldPrintMessage) {
77+
originalMethod(format, ...args)
78+
}
79+
7580
// Capture the call stack now so we can warn about it later.
7681
// The call stack has helpful information for the test author.
7782
// Don't throw yet though b'c it might be accidentally caught and suppressed.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = () => {
2+
console.error('my error message that I do not control');
3+
throw new Error('some other error');
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const consoleError = require('.')
2+
3+
describe('print message flag with other test failure', () => {
4+
it('does not throw', () => {
5+
expect(consoleError).not.toThrow()
6+
})
7+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
setupFilesAfterEnv: ['./jest.setup.js'],
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const failOnConsole = require('../../..')
2+
3+
failOnConsole({
4+
shouldFailOnError: true,
5+
shouldPrintMessage: true,
6+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = () => {
2+
console.error('my error message that I do not control')
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const consoleError = require('.')
2+
3+
describe('print message flag', () => {
4+
it('does not throw', () => {
5+
expect(consoleError).not.toThrow()
6+
})
7+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
setupFilesAfterEnv: ['./jest.setup.js'],
3+
}

0 commit comments

Comments
 (0)