开玩笑地测试用户API

时间:2018-11-30 11:54:55

标签: javascript testing jestjs supertest

我正在尝试测试用户API的注册和登录路由。我正在使用Jest和supertest。我进行了一些失败的尝试,但所有尝试均以相同的错误完成:

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',

这是代码:

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

我正在使用相同的方法来测试其他API,并且效果很好。

0 个答案:

没有答案