Jest Expect.assertions()方法在异步函数上失败

时间:2019-05-29 15:19:09

标签: reactjs jestjs

我正在尝试使用Jest测试提取调用,并且正在使用在教程中看到的expect.assertions()方法。本教程使用的示例当前是fetchUser()对象中的方法testingFunctions

唯一的区别是fetchUser()fetchUser3()是前者使用axios。

我从测试中得到的错误是: Expected one assertion to be called but received zero assertion calls.

const testingFunctions = {
  fetchUser: () =>
    axios
      .get("https://jsonplaceholder.typicode.com/users/2")
      .then(response => response.data)
      .catch(err => "invalid fetch"),
  fetchUser3: () => {
    fetch("https://jsonplaceholder.typicode.com/users/3")
      .then(response => response.json())
      .catch(err => "Oh no");
  }
};
test('should fetch and return an object with name of "Clementine Bauch"', () => {
  expect.assertions(1);
  return testingFunctions.fetchUser3().then(data => {
    expect(data.name).toEqual("Clementine Bauch");
  });
});

有什么想法吗?

0 个答案:

没有答案