1

I am trying to write a test for an async function.

Based on suggestions online and documentation: https://jestjs.io/docs/asynchronous

I have tried all the following test variations. Ends up with following error for all tests.

ReferenceError: regeneratorRuntime is not defined

I never hit the breakpoints in the actual async function.
Able to write tests fine if I make this function non async (just to validate the test).
Could I pls get some guidance on what I am doing wrong pls.

The async call is due to calling this cookieStore.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies/CookieStore
Need it cos I need the expiry value for cookie.

This is the async function I am testing.

const checkData = async () => {

  const cookie = await cookieStore.get('sample');
  const {expires} = { ...cookie };

  // other logic here which may result in returning a false;

  return true;
}

These are the tests where every one of them throws same above error.

describe('do Test', () => {

  it('should be false when session data exists', async () => {
    const result = checkData();
    expect(result).toBe(true);
  });

  it('should be false when session data exists', async () => {
    const result = checkData();
    return expect(result).toBe(true);
  });

  it('the data is peanut butter', done => {
    const callback = data => {
        expect(data).toBe(true);
        done();
    };

    checkData(callback);
  });

  it('the data is peanut butter1', () => {
    return checkData().then(data => {
      expect(data).toBe(true);
    });
  });

  it('the data is peanut butter2', () => {
    return expect(checkData()).resolves.toBe(true);
  });

  it('the data is peanut butter3', async () => {
    const data = await checkData();
    expect(data).toBe(true);
  });

  it('the data is peanut butter4', async () => {
    await expect(checkData()).resolves.toBe(true);
  });
});
Fllappy
  • 371
  • 3
  • 11
  • Have you tried to import regeneration runtime, [this answer](https://stackoverflow.com/questions/42535270/regeneratorruntime-is-not-defined-when-running-jest-test/57439821#57439821) can help – muzazu Aug 25 '21 at 21:50

0 Answers0