🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Testing Troubles with Jest and ESM and how to fix it

↗ Quelle (dev.to)
🗣️ Stimme:

This week, I was tasked with adding tests to my 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:



Here is a quick overview of the setup:



Install Jest:




CODE
npm install --save-dev jest






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




CODE
export default {
testPathIgnorePatterns: ["/node_modules/", "/examples"],
transform: {},
};






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




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






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




CODE
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);
});
});






If you want to test using a single test file:




CODE
npm test -- banner.test.js






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




CODE
npm run test






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:




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






See (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:




CODE
import { http, HttpResponse } from 'msw'

export const handlers = [
http.post('https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent', () => {
return HttpResponse.json({
candidates: [
{
content: {
parts: [
{
text: JSON.stringify({
explanation: "The provided code has been refactored to address several issues and improve readability.",
refactoredCode: `
var count = 0;
var message = "Hello";
`
})
}
],
role: "model"
},
finishReason: "STOP",
index: 0
}
],
});
}),
];







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:




CODE
import { setupServer } from "msw/node";
import { handlers } from "../mocks/handler.js";
export const server = setupServer(...handlers);

server.listen();






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.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Testing Troubles with Jest and ESM and how to fix it

Thematisch verwandte Begriffe: Testing, Troubles, with, Jest · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...