Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout

I'm starting to test my code with Jest, and I can't make a seemingly simple test to pass. I am simply trying to check if what I receive from a Maogoose database request is an object.

The function fetchPosts() is working because I hooked it up with a React frontend and it is displaying the data correctly.

This is my function fetchPosts():

module.exports = {
    fetchPosts() {
        return new Promise((resolve, reject) => {
            Posts.find({}).then(posts => {
                if (posts) {
                    resolve(posts)
                } else {
                    reject()
                }
            })
        })
    }
}

And my test:

it('should get a list of posts', function() {
    return posts.fetchPosts().then(result => {
        expect(typeof result).toBe('object')
    })
})

This makes the test fail, and Jest says

'Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.'

QUESTION: How can I make this test pass?

You can expect asynchronous results using resolves, as shown in the Jest documentation.

In your case:

it('should get a list of posts', function() {
    const result = posts.fetchPosts();
    expect(result).resolves.toEqual(expect.any(Object));
})

…although I have a suspicion your list of posts is actually an array, so you probably want this:

it('should get a list of posts', function() {
    const result = posts.fetchPosts();
    expect(result).resolves.toEqual(expect.any(Array));
})

Another tip: You don't need to wrap the body of your fetchPost in an additional promise, you can simply return the promise you get from Posts.find and add a then to it, like this:

module.exports = {
    fetchPosts() {
        return Posts.find({}).then(posts => {
            if (posts) {
                return posts;
            } 
            throw new Error('no posts'); // this will cause a promise rejection
        })
    }
}

Async callback was not invoked within the 5000ms timeout, Error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout. However I can see that the tests do  Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout. However I can see that the tests do indeed finish and are not long running.

Also if you are simply looking to increase the timeout, then you can do that by setting

 jest.setTimeout(10000);

Timeout, setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: 3 | test('Timeout on the  JEST: Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout; Got Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout; However, I implemented all 3 fixes suggested; Use jest.setTimeout() inside the test to set the async timeout

It's also highly possible that you're not getting a response back from the DB at all from your test suite. Test suite's can call different environmental variables / configs that lead to different calls. This error can also be seen if no response is returned, as in - if someone blocks your IP from connecting, on and on.

Jest testing with Node - Timeout, setTimeout – This error will be thrown if an async method invocation takes more that 5 seconds. I have been using Jest.js to test my Node  : Timeout -Async callback was not invoked within the 5000ms timeout specified by jest. setTimeout. Timeout - Async callback was not invoked within the 5000ms timeout specified by jest . setTimeout .

Troubleshooting · Jest, Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout - javascript. Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout Ask Question Asked 1 year, 4 months ago

Testing Asynchronous Code · Jest, This will run Jest in a Node process that an external debugger can connect to. Note that There are multiple ways to debug Jest tests with Visual Studio Code's built in debugger. To attach Error: Timeout - Async callback was not invoked within timeout specified by jasmine. jest.setTimeout(10000); // 10 second timeout. I am getting Timeout - Async callback was not invoked within the 500000ms timeout specified by jest.setTimeout. Please provide your exact Jest configuration. My testing is currently running inside docker container. test

Cannot test a simple route with Jest : node, It's common in JavaScript for code to run asynchronously. When you have The most common asynchronous pattern is callbacks. For example If done() is never called, the test will fail (with timeout error), which is what you want to happen. If the expect statement fails, it throws an error and done() is not called. If we want to  Got Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout; However, I implemented all 3 fixes suggested; Use jest.setTimeout() inside the test to set the async timeout; Use the third parameter of test() to pass in an extended async timeout limit; Call the done function when complete; However, when