【问题标题】:Async test in jest tutorial笑话教程中的异步测试
【发布时间】:2019-01-12 08:45:44
【问题描述】:

您好,我正在尝试与本示例教程相同,但它给了我错误。

https://jestjs.io/docs/en/tutorial-async

  expect(received).toEqual(expected)

    Expected value to equal:
      "Paul"
    Received:
      undefined

    Difference:

      Comparing two different types of values. Expected string but received undefined.

      14 | it('works with async/await and resolves', async () => {
      15 |   expect.assertions(1);
    > 16 |   await expect(user.getUserName(5)).resolves.toEqual('Paul');
         |                                                ^
      17 | });

一切都一样,我的服务器返回一个 json,但开玩笑说 http get 后未定义

{"id":10002,"name":"test","passportNumber":"A1234568"}

【问题讨论】:

  • 我会监视 getUserName 并返回一个模拟的 Promise。测试不应该做 HTTP 请求,这些应该被模拟。
  • 同教程
  • 教程代码不起作用

标签: reactjs react-native jestjs


【解决方案1】:

它返回 undefined 因为源函数不是一个承诺函数。这是简单的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2022-08-19
    • 2018-12-19
    • 2018-02-22
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多