Home > Web Front-end > JS Tutorial > Testing Troubles with Jest and ESM and how to fix it

Testing Troubles with Jest and ESM and how to fix it

Linda Hamilton
Release: 2024-11-09 22:05:02
Original
804 people have browsed it

Testing Troubles with Jest and ESM and how to fix it

This week, I was tasked with adding tests to my RefactorCode project. As I started adding new features, I noticed that some existing features were breaking, and it became increasingly difficult to test everything manually. This made it clear that I needed to implement proper automated testing to ensure the application is stable.

Since I had prior experience with Jest from my previous Cloud Computing for Programmers course, where I worked with Node.js, I decided to use it for this project as well. I started by writing some tests to check the functionality of my code. However, I encountered a few errors that I hadn’t faced before. After some debugging and searching through Stack Overflow, I realized that there are additional configurations required when using ESM (ECMAScript Modules) with Jest. In my previous project, I had used CommonJS, which worked perfectly. While I could have opted to use Babel for the conversion, Jest offered a new beta feature that allowed ESM to run directly. I decided to give it a try, and it worked great!

This explains everything related to configuring jest with ESM: https://jestjs.io/docs/ecmascript-modules

Here is a quick overview of the setup:

Install Jest:

npm install --save-dev jest
Copy after login
Copy after login

Create a jest.config.js file. Here I set what folders to ignore as well:

export default {
  testPathIgnorePatterns: ["/node_modules/", "/examples"],
  transform: {},
};
Copy after login
Copy after login

In the package.json scripts section, use the experimental argument for jest to work with ESM modules:

"scripts": {
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}
Copy after login

Create a test file. I created all the tests within a test folder in root directory. Here is a simple one I created:

import { readFile, checkIfDirectory } from "../src/fileUtils";

describe("File Utility", () => {
    test("Read File", () => {
      readFile("./examples/test.txt").then((data) => {
        expect(data).toBe("Hello World");
      });

    });

    test('should return true if the path is a directory', async () => {
      const result = await checkIfDirectory('./examples');
      expect(result).toBe(true);
    });
});
Copy after login

If you want to test using a single test file:

npm test -- banner.test.js
Copy after login

For running all the tests, we use the script, we added earlier in package.json:

npm run test
Copy after login

Finally, the basic tests were set up. However, this was just the beginning of my troubleshooting journey. I also faced difficulties with mocking libraries and modules, especially since they were using ESM. These required extra configuration, but after some tweaking, I was able to get everything working, and the tests ran successfully.

Instead of using the regular jest.mock, you have to use jest.unstable_mockModule:

For example: await jest.unstable_mockModule("fs", () => ({
  existsSync: jest.fn(),
  readFileSync: jest.fn(),
}));
Copy after login

See this section for more info.

One of the core functionalities I wanted to test involved integrating with the Gemini API. Since I didn’t want to rely on the live API for testing, I decided to mock the API calls. Initially, I tried using Nock, but I ran into issues because it didn’t work well with Node’s default fetch implementation. It seemed that the Gemini library was using the default fetch instead of a third-party fetch implementation, which caused the mock to fail. After several attempts without success, I switched to another library called MSW (Mock Service Worker). Although it required a bit more setup to create a mock server, it worked flawlessly on the first try. I crafted an example response based on how the Gemini API would respond, which allowed me to test the refactored functionality.

Here is how the mock server was set up for reference:

npm install --save-dev jest
Copy after login
Copy after login

You can see that I am mocking a specific API endpoint that I am using for the application. Now all you have to do this add the below to start the server while testing:

export default {
  testPathIgnorePatterns: ["/node_modules/", "/examples"],
  transform: {},
};
Copy after login
Copy after login

Reflecting on the process, this was a great learning experience for me. In hindsight, I probably should have checked the compatibility of the libraries I was using and considered any potential issues beforehand. Despite the challenges, I’m glad to have reached a point where my tests are running smoothly. I look forward to adding more tests in the future and improving the overall stability of my project.

The above is the detailed content of Testing Troubles with Jest and ESM and how to fix it. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template