I’m trying to integrate axios into my test environment. So far, however, I get the error below when starting the test. I use React testing library with Jest
table.test.js:
import { expect, jest, test } from "@jest/globals";
import { fireEvent, render, screen } from "@testing-library/react";
import axios from "axios";
import "@testing-library/jest-dom";
jest.mock("axios", () => ({
__esModule: true,
default: {
get: () => ({
data: { id: 1, name: "Ben" },
}),
},
}));
Cannot find module ‘axios’ from ‘src/components/Table/table.test.js’
dependencies
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
>Solution :
You could use the axios-mock-adapter NPM package for this instead of making your own axios mock: https://github.com/ctimmerm/axios-mock-adapter