1

In my react application i am trying to write e2e testing using with puppeteer (jest).

test.js

  const puppeteer = require("puppeteer");

    let browser;
    let page;

    describe("LoginPage", () => {
     beforeAll(async () => {
     browser = await puppeteer.launch({
     headless: false,
     slowMo: 250
    });
      page = await browser.newPage();
    });

   it("should display login page", async () => {
     await page.goto("http://localhost:3000/");
     const text = await page.evaluate(() => document.body.innerText);
     expect(text).toContain("Unity");
   });

    it("should show error message if email is not correct", async() => {
      await page.type("#email", "user");
      await page.type("#password", "pass");
      await page.click("button[type=submit]";

            const errorMessage = await page.evaluate(
            async () =>
             document.getElementsByClassName("ui negative message container")[0]
            );
          console.log("errorMessage", errorMessage);
       });
    });

But this e2e testing couldn't make it work.

I am receiving an error :

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Can somebody help me to solve this issue?

1
  • You can try to change default jest's timeout (which is 5 sec) by adding: jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000; to a setupTestFrameworkScriptFile.js file, which you address from jest config: setupTestFrameworkScriptFile: require.resolve('./setupTestFrameworkScriptFile.js') - Plus, you can look at chromium and see where the test is stuck. It will help you find the root cause Commented Feb 20, 2018 at 13:23

1 Answer 1

1

With jest v23, I'd try a timeout and some minor editions of your code (check the comments):

const puppeteer = require("puppeteer");

let browser;
let page;

describe("LoginPage", () => {
  beforeAll(async() => {
    browser = await puppeteer.launch({
      headless: false,
      slowMo: 250
    });
    page = await browser.newPage();
  }, 60e3); // timeout

  afterAll(() => browser.close()); // <-- don't leave browser hanging after the suite

  beforeEach(() => page.goto("http://localhost:3000")); // start with fresh page between test, don't keep implicit page state dependency

  it("should display login page", async() => {
    const text = await page.evaluate(() => document.body.innerText);
    expect(text).toContain("Unity");
    done(); //here
  });

  it("should show error message if email is not correct", async() => {
    await page.type("#email", "user");
    await page.type("#password", "pass");
    await page.click("button[type=submit]");

    try { // you need to try catch the error with async await
      await page.evaluate(
        () => document.getElementsByClassName("ui negative message container")[0] // no need for async
      );
    } catch (errorMessage) {
      console.log("errorMessage", errorMessage);
    }
    
    // you lack the expectation in the test
    // expect(true).toBe(true)
  });
});

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your quick reply. But again same error.
@Khushi I didn't know it was jest, as far as I remember it was not indicated in the initial question. In which case please provide a sample repo with verifiable data, so I can reproduce locally.
@Khushi meanwhile please recheck the code, I updated it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.