jestjsjest-puppeteer

Jest-Puppeteer test randomly fails without output


I have the following testcase:

it('User can logout', async () => {
    await helper.navClick('.header-user-action__logout');
    const url = page.url();
    console.log(url)
    await expect(url).toContain('auth');
});

helper.navClick is just a small wrapper:

async function navClick(selector, options) {
    return await Promise.all([page.waitForNavigation(), expect(page).toClick(selector, options)]);
}

Most of the time, it succeeds without any problem, but sometimes it'll be marked as failed:

    ✕ User can logout (569 ms)

  ● Login › User can logout





  console.log
    https://auth.example.com/auth/realms/example/protocol/openid-connect/auth?response_type=code&client_id=example-app&redirect_uri=https%3A%2F%2Fexample.com%2Fsso%2Flogin&state=807469fd-3ee5-4d93-8354-ca47a63e69a6&login=true&scope=openid

How can this happen? The url contains "auth" multiple times, and I don't see anything else that could cause the test to fail.


Solution

  • I found the issue by setting up a custom environment:

    const PuppeteerEnvironment = require("jest-environment-puppeteer");
    const util = require('util');
    
    class DebugEnv extends PuppeteerEnvironment {
    
        async handleTestEvent(event, state) {
            const ignoredEvents = ['setup', 'add_hook', 'start_describe_definition', 'add_test', 'finish_describe_definition', 'run_start',
                'run_describe_start', 'test_start', 'hook_start', 'hook_success', 'test_fn_start', 'test_fn_success', 'test_done',
                'run_describe_finish', 'run_finish', 'teardown'];
            if (!ignoredEvents.includes(event.name)) {
                console.log(new Date().toString() + " Unhandled event(" + event.name + "): " + util.inspect(event));
            }
        }
    }
    
    module.exports = DebugEnv;
    

    In my package.json, I set the testEnvironment to this DebugEnv:

    "jest": {
        "preset": "jest-puppeteer",
        "testEnvironment": "./debugenv.js",
    

    By doing this, I found an error which had nothing to with the test itself (network related if I recall correctly).