Jonas S.
Jonas S.

Reputation: 133

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.

Upvotes: 6

Views: 916

Answers (2)

Mark Terrel
Mark Terrel

Reputation: 126

Unfortunately, this failure with no message is the default behavior you see when you're using jest-environment-puppeteer and the page in the browser has an unhandled global error thrown.

You can disable this behavior by setting exitOnPageError to false in your jest-puppeteer.config.js:

module.exports = {
  exitOnPageError: false,
};

You can get more useful behavior by adding your own listener to "pageerror" like this:

page.on("pageerror", (err) => {
  // Print the error
  console.log("Unhandled browser error:", err);

  // Cause Jest to fail the test
  process.emit('uncaughtException', err);
});

Upvotes: 2

Jonas S.
Jonas S.

Reputation: 133

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).

Upvotes: 5

Related Questions