diff --git a/index.js b/index.js index da5b031..1af1f9f 100644 --- a/index.js +++ b/index.js @@ -125,6 +125,12 @@ const init = ({ } let shouldSkipTest + beforeAll(() => { + flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks) + }) + + console[methodName] = newMethod + beforeEach(() => { shouldSkipTest = canSkipTest() if (shouldSkipTest) return diff --git a/tests/fixtures/error-before-beforeEach/index.test.js b/tests/fixtures/error-before-beforeEach/index.test.js new file mode 100644 index 0000000..2a3a188 --- /dev/null +++ b/tests/fixtures/error-before-beforeEach/index.test.js @@ -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. + }) +}) diff --git a/tests/fixtures/error-before-beforeEach/jest.config.js b/tests/fixtures/error-before-beforeEach/jest.config.js new file mode 100644 index 0000000..8fcc29f --- /dev/null +++ b/tests/fixtures/error-before-beforeEach/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + setupFilesAfterEnv: ['./jest.setup.js'], +} diff --git a/tests/fixtures/error-before-beforeEach/jest.setup.js b/tests/fixtures/error-before-beforeEach/jest.setup.js new file mode 100644 index 0000000..a8ae86a --- /dev/null +++ b/tests/fixtures/error-before-beforeEach/jest.setup.js @@ -0,0 +1,5 @@ +const failOnConsole = require('../../..') + +failOnConsole() + +console.error('console error message out in the wild') diff --git a/tests/index.test.js b/tests/index.test.js index 7d551c4..f7e7984 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -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'); + }) })