Skip to content

Commit

Permalink
Before runs flush out and report console calls (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedidomizio authored Oct 1, 2024
1 parent 15290be commit bd52716
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ const init = ({
}
let shouldSkipTest

beforeAll(() => {
flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks)
})

console[methodName] = newMethod

beforeEach(() => {
shouldSkipTest = canSkipTest()
if (shouldSkipTest) return
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/error-before-beforeEach/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe('console.error', () => {
it('does not throw', () => {
// Expected to be empty as we want to ensure the test works with a call outside of tests
// and not something that happens during testing.
})
})
3 changes: 3 additions & 0 deletions tests/fixtures/error-before-beforeEach/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js'],
}
5 changes: 5 additions & 0 deletions tests/fixtures/error-before-beforeEach/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const failOnConsole = require('../../..')

failOnConsole()

console.error('console error message out in the wild')
7 changes: 7 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ describe('jest-fail-on-console', () => {
expect(stdout).toContain('console.error');
expect(stdout).toContain('my error message that I do not control');
})

it('errors when console.error() called before testing has begun', async () => {
const { stderr } = await runFixture('error-before-beforeEach')

expect(stderr).toContain('console.error');
expect(stderr).toContain('console error message out in the wild');
})
})

0 comments on commit bd52716

Please sign in to comment.