本文介绍了对于调用另一个异步函数的异步函数,Jest测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试使用另一个异步函数返回的数据的异步函数.这是解释我的问题的代码:

I am trying to test an async function that uses data returned by another async function. Here is the code to explain my problem:

StudentInfo.js

export async function getData() {
  //studentData imported from another file
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(studentData);
    }, 5000);
  });
}

export async function filterStudents() {
  const studentData = await StudentInfo.getData();

  //computations

  return filteredData; //object
}

StudentInfo.test.js

import * as studentInfo from "src/StudentInfo.js"

describe("StudentInfo" , () => {
   test("student data correctly filtered", async () => {
      const studentData = [
         {
            name: Sarah Marshall,
            id: srhmar451
         },
         {...}
      ];
      expectedData = { [...], [...]};
      const spy = jest.spyOn(studentInfo, "getData");
      spy.mockReturnValue(studentData);
      await expect(studentInfo.filterStudents()).toEqual(expectedData);
   });
});

我的测试失败,因为期望的返回值为Promise {}.有人可以帮我为我的filterStudents()函数编写测试吗?我被这个问题困扰了太久了.

My test fails because the expected return value is Promise {}. Could someone help me write a test for my filterStudents() function? I have been stuck at this for too long.

推荐答案

以下是一个使您入门的有效示例:

Here is a working example to get you started:

StudentInfo.js

StudentInfo.js

import * as StudentInfo from './StudentInfo';

export async function getData() {
  throw new Error('should not get here');  // <= getData will be mocked
}

export async function filterStudents() {
  const studentData = await StudentInfo.getData();
  return studentData.filter(v => v.id === '2');
}

StudentInfo.test.js

StudentInfo.test.js

import * as StudentInfo from "./StudentInfo"

describe("StudentInfo", () => {
  test("student data correctly filtered", async () => {
    const studentData = [
      { name: 'student1', id: '1' },
      { name: 'student2', id: '2' },
      { name: 'student3', id: '3' }
    ];
    jest.spyOn(StudentInfo, "getData").mockResolvedValue(studentData);
    await expect(StudentInfo.filterStudents()).resolves.toEqual([{ name: 'student2', id: '2' }]);  // Success!
  });
});

您可以使用 mockFn.mockResolvedValue 来模拟返回值作为解析为您传递给它的值的Promise(尽管如果您只使用 mockFn.mockReturnValue ,因为对某个值(例如const five = await 5; // <= works fine)调用await是有效的.

You can use mockFn.mockResolvedValue to mock the return value as a Promise that resolves to the value you pass to it (although it also works fine if you just use mockFn.mockReturnValue since it is valid to call await on a value, for example const five = await 5; // <= works fine).

重要的部分是使用 .resolves 来告诉Jest期望值是您希望解析的Promise,然后链接toEqual以对期望的解析值进行断言.

The important part is to use .resolves to tell Jest that the expected value is a Promise that you expect to resolve, and then chain the toEqual to assert on the expected resolved value.

这篇关于对于调用另一个异步函数的异步函数,Jest测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:09