jestjs – ReactJS, test data endpoints [SOLVED]
here I want to test the data output on the endpoint.
My Code test =
import { render, screen } from "@testing-library/react";
import axios from "axios";
import CompBuildingGeo from "./components/CompBuildingGeo";
import { getBuildingGEO } from "../../service/building";
const buildingGeo = async () => {
try {
const result = await getBuildingGEO();
console.log(result?.map((total) => total.total_building));
} catch (error) {
console.log("salah");
} finally {
console.log("none");
}
};
describe("Component Building", () => {
test("CompBuildingGeo API", async () => {
render(<CompBuildingGeo />);
jest.spyOn(axios, "get").mockReturnValue({
data: [
{
total_building: "76915222",
},
],
});
const result = await buildingGeo();
expect(result).toBe("76915222");
});
});
However, when I “npm run test”, this error appears.
Expected: "76915222"
Received: undefined
35 | });
36 | const result = await buildingGeo();
> 37 | expect(result?.map((total) => total.total_buildings)).toBe("76915222");
|
38 | });
39 |});
what is the cause of the error? can you guys help me? Thank You
Read more here: Source link
