Message “Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout”

后端 未结 16 1494
[愿得一人]
[愿得一人] 2020-11-28 02:56

I\'m using Puppeteer and Jest to run some front end tests.

My tests look as follows:

describe("Profile Ta         


        
相关标签:
16条回答
  • 2020-11-28 03:31
    // In jest.setup.js
    jest.setTimeout(30000)
    

    If on Jest <= 23:

    // In jest.config.js
    module.exports = {
      setupTestFrameworkScriptFile: './jest.setup.js'
    }
    

    If on Jest > 23:

    // In jest.config.js
    module.exports = {
      setupFilesAfterEnv: ['./jest.setup.js']
    }
    
    0 讨论(0)
  • 2020-11-28 03:32

    The answer to this question has changed as Jest has evolved. Current answer (March 2019):

    1. You can override the timeout of any individual test by adding a third parameter to the it. I.e., it('runs slow', () => {...}, 9999)

    2. You can change the default using jest.setTimeout. To do this:

      // Configuration
      "setupFilesAfterEnv": [  // NOT setupFiles
          "./src/jest/defaultTimeout.js"
      ],
      

      and

      // File: src/jest/defaultTimeout.js
      /* Global jest */
      jest.setTimeout(1000)
      
    3. Like others have noted, and not directly related to this, done is not necessary with the async/await approach.

    0 讨论(0)
  • 2020-11-28 03:32

    Make sure to invoke done(); on callbacks or it simply won't pass the test.

    beforeAll((done /* Call it or remove it */ ) => {
      done(); // Calling it
    });
    

    It applies to all other functions that have a done() callback.

    0 讨论(0)
  • 2020-11-28 03:32

    In case someone doesn't fix the problem use methods above. I fixed mine by surrounding the async func by an arrow function. As in:

    describe("Profile Tab Exists and Clickable: /settings/user", () => {
        test(`Assert that you can click the profile tab`, (() => {
          async () => {
            await page.waitForSelector(PROFILE.TAB)
            await page.click(PROFILE.TAB)
          }
        })(), 30000);
    });
    
    0 讨论(0)
  • 2020-11-28 03:33

    The timeout problem occurs when either the network is slow or many network calls are made using await. These scenarios exceed the default timeout, i.e., 5000 ms. To avoid the timeout error, simply increase the timeout of globals that support a timeout. A list of globals and their signature can be found here.

    For Jest 24.9

    0 讨论(0)
  • 2020-11-28 03:35

    It should call the async/await when it is async from test.

    describe("Profile Tab Exists and Clickable: /settings/user", () => {
        test(`Assert that you can click the profile tab`, async (done) => {
            await page.waitForSelector(PROFILE.TAB);
            await page.click(PROFILE.TAB);
            done();
        }, 30000);
    });
    
    0 讨论(0)
提交回复
热议问题