【问题标题】:Jest tests passed when in promises, but failed in async/awaitJest 测试在 Promise 中通过,但在 async/await 中失败
【发布时间】:2018-06-11 10:46:21
【问题描述】:

我正在使用 Jest + supertest 在我的 RESTful API 上编写测试。 我的test.js 一开始是这样的:

const crypto = require('crypto')
const request = require('supertest')
const app = require('./app')

const genUUID = () => {
  return ([1e7]+1e3+4e3+8e3+1e11).replace(/[018]/g, c =>
    (c ^ crypto.randomFillSync(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  )
}

let uuid1 = genUUID()
let uuid2 = genUUID()


describe('Test /uuids', () => {
  it('Get list of uuids', () => {
    request(app).get('/api/uuids/').then(res =>
      expect(res.statusCode).toBe(200)
    )
  })
})

describe('Test /uuids/:uuid', () => {
  it('Get info of a not-existed product', () => {
    request(app).get('/api/uuids/' + uuid1).then(res =>
      expect(res.statusCode).toBe(400)
    )
  })
})

它可以工作并且所有测试都通过了。

但我喜欢 async/await 的风格,所以我将 Promise 切换为 async/await。

... // The previous part remains unchanged
describe('Test /uuids', () => {
  it('Get list of uuids', async() => {
    const res = await request(app).get('/api/uuids/')
    expect(res.statusCode).toBe(200)
  })
})

describe('Test /uuids/:uuid', () => {
  it('Get info of a not-existed product', async () => {
    const res = await request(app).get('/api/uuids/' + uuid1)
    expect(res.statusCode).toBe(400)
  })
})

这一次出现了错误。

console.error api/uuids.js:247
    ERR!error: bind message supplies 1 parameters, but prepared statement "Get lists of UUIDs" requires 6
    ....

 ● Test /uuids/:uuid › Get info of a not-existed product

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

我是否正确编写了异步/等待?或者 Jest 中的 async/await 有什么问题吗?

附: node 版本为v8.11.2

【问题讨论】:

  • 我怀疑第一个工作是否符合您的预期。如果期望没有通过测试,它自己将保持绿色,因为 jest 已经完成了测试。
  • 可能会更改第一个版本,以便它返回承诺并测试它们是否有效。顺便说一句,您的异步/等待解决方案看起来不错。所以也许supertest 不是你所期望的。
  • 在您的第一个版本中,it 回调永远不会返回承诺。也许这就是为什么它报告它们为通过,甚至没有查看结果。
  • 也许您需要致电.end() 发送请求,因为您不使用来自超测的expect
  • @AndreasKöberle @Bergi 你是对的。当我添加return 时,promise 不起作用,它会抛出与 async/await 相同的异常。原来是 API 代码的问题,而不是玩笑的问题

标签: javascript async-await jestjs


【解决方案1】:

promise 版本实际上不起作用。正如在 cmets(thisthis)中所说,测试结果被跳过,给人一种通过测试的错误感觉。在 Promise 中添加 return 会导致与 async/await 情况相同的错误。

通过模拟应用代码

const app = new (require('express'))()

app.get('/api/uuids', (req, res) => {
  res.status(200).end()
})

app.get('/api/uuids/:uuid', (req, res) => {
  res.status(200).end()
})

module.exports = app

async/await 可以判断 GET /api/uuids/:uuid 中存在某种错误,而 promise 仍然报告所有测试都已通过。所以 async/await 是正确的。

经过一番调试,原来是我的代码查询数据库的问题,所以一开始就抛出了postgres错误。

【讨论】:

    【解决方案2】:

    尝试增加超时限制(通过以毫秒为单位的数字作为第二个参数传递给it 函数):

    describe('Test /uuids', () => {
      it('Get list of uuids', async() => {
        const res = await request(app).get('/api/uuids/')
        expect(res.statusCode).toBe(200)
      }, 30000)
    })
    
    describe('Test /uuids/:uuid', () => {
      it('Get info of a not-existed product', async () => {
        const res = await request(app).get('/api/uuids/' + uuid1)
        expect(res.statusCode).toBe(400)
      }, 30000)
    })
    

    【讨论】:

      猜你喜欢
      • 2020-05-30
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多