vendredi 30 novembre 2018

Jest testing user API

I am trying to test the register and login routes for an user API. I am using Jest and supertest. I had some failing attempts but all of them finished with the same error:

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

   9 | describe('Test Users routes - route /register', () => {
  10 |
> 11 |      it('should succesfully register a valid user', async() => {
     |      ^
  12 |              const newUser = new User({
  13 |                      name: 'newuser',
  14 |                      email: 'new@gmail.com',

This is the code:

    it('should succesfully register a valid user', async() => {
        const newUser = new User({
            name: 'newuser',
            email: 'new@gmail.com',
            password: 'new123'

        })
        const expectedName = 'newuser'
        const ExpectedEmail = 'new@gmail.com'
        const newUserStub = 
     sinon.stub(User.prototype,'save').resolves(newUser)
    const fakeResponse = await request(app).post('/api/users/register').expect(200)
    expect(fakeResponse.body.name).toBe(expectedName.trim())
    expect(fakeResponse.body.email).toBe(ExpectedEmail.trim())
    //existingUserStub.restore()
    newUserStub.restore()
})

I am using the same method to tests other API's and it is working just fine.

Aucun commentaire:

Enregistrer un commentaire