admin管理员组

文章数量:1193778

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?

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?

Share Improve this question edited Nov 20, 2018 at 17:15 Patrick Hund 20.2k12 gold badges70 silver badges95 bronze badges asked Nov 20, 2018 at 16:46 mokiliii Lomokiliii Lo 6373 gold badges14 silver badges27 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14

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

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.

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

 jest.setTimeout(10000);

You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.

For me none of the above worked so I tried older version of jest and it worked npm i -D [email protected]. if you are using it with typescript make sure to degrade ts-jest as well npm i -D [email protected] [email protected]

本文标签: