Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use Axios(Mock) in test environment

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading