ValentinH / jest-fail-on-console

Utility to make jest tests fail when console.error() or any other methods are used
MIT License
141 stars 20 forks source link

We should only override console methods before each test #5

Closed NikhilVerma closed 2 years ago

NikhilVerma commented 2 years ago

This fixes the issue where this utility will swallow any console error methods logged outside the test environments. e.g. errors logged BEFORE setup/teardown of Jest tests.

I am also using the more native jest.spyOn to spy on the tests.

ValentinH commented 2 years ago

Could you please explain a bit more about "swallow any console error methods logged outside the test environments" please? Do you have an example of such an issue?

NikhilVerma commented 2 years ago

@ValentinH absolutely! here is a rough example

// Any errors thrown here will never be shown to the user
throwConsoleError();

describe("my-test", () => {
    it("fails on console", () => {
        expect.assertions(1);
        // This will work fine
        throwConsoleError();
        expect(1).toStrictEqual(1);
    });
});

function throwConsoleError() {
    console.error("I am a console error");
}
ValentinH commented 2 years ago

I just tested it and it works great! Thanks for the fix 🙏

ValentinH commented 2 years ago

Published as 2.0.5 🚀