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);
});
});
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.
spy.mockResolvedValue(studentData)