Windsurf testing

Jest Mocking Not Working with ESM in Windsurf Project

Jest mocking doesn't work in your Windsurf-generated project that uses ES modules (ESM). jest.mock() calls are silently ignored, mocked modules still return real implementations, or you get errors about import statements not being mockable. Tests that should use mocked dependencies are hitting real APIs or databases.

This is one of the most common testing frustrations in modern JavaScript projects. Jest was designed for CommonJS (require), and its mocking system doesn't work the same way with ESM imports. Cascade generates test files with jest.mock() patterns that only work in CommonJS, leading to tests that appear to set up mocks correctly but actually use the real modules.

You might notice this when tests make real HTTP requests, modify your actual database, or fail with authentication errors — all signs that mocking isn't working and real dependencies are being used.

Error Messages You Might See

jest.mock() is not allowed in ESM The module factory of jest.mock() is not allowed to reference any out-of-scope variables SyntaxError: Cannot use import statement outside a module Mock not working: received real implementation instead of mock TypeError: jest.unstable_mockModule is not a function
jest.mock() is not allowed in ESMThe module factory of jest.mock() is not allowed to reference any out-of-scope variablesSyntaxError: Cannot use import statement outside a moduleMock not working: received real implementation instead of mockTypeError: jest.unstable_mockModule is not a function

Common Causes

  • jest.mock() hoisting doesn't work with ESM — In CommonJS, Jest hoists jest.mock() calls to the top of the file. With ESM, import statements are evaluated before any code runs, so mocks aren't in place when imports execute
  • Missing babel/ts-jest transformation — Without proper transformation, import statements aren't converted to requires, and Jest's mock system can't intercept them
  • Using import instead of require in mock setup — Mocked modules imported with static import get the real module, not the mock
  • jest.config using wrong transform — The Jest configuration doesn't have the correct transform for .ts or .tsx files with ESM syntax
  • Mock factory function returning wrong shape — The mock factory doesn't match the module's export shape (named exports vs default export)

How to Fix It

  1. Use jest.unstable_mockModule for ESM — Replace jest.mock() with jest.unstable_mockModule() and use dynamic import() after setting up mocks
  2. Configure ts-jest or babel-jest properly — Ensure your Jest config transforms ESM to CJS: transform: { '^.+\\.tsx?$': 'ts-jest' } with useESM: true in ts-jest config
  3. Use dependency injection instead — Refactor code to accept dependencies as parameters rather than importing them directly. This makes testing trivial regardless of module system
  4. Match mock shape to real module — For named exports, return an object with all exported names. For default exports, use { __esModule: true, default: mockFn }
  5. Consider Vitest as an alternative — Vitest has native ESM support and a Jest-compatible API. Migration is often straightforward and fixes ESM mocking issues
  6. Use manual mocks in __mocks__ directory — Create a __mocks__/module-name.ts file that Jest automatically uses. This approach works with both CJS and ESM

Real developers can help you.

Victor Denisov Victor Denisov Developer Nam Tran Nam Tran 10 years as fullstack developer Prakash Prajapati Prakash Prajapati I’m a Senior Python Developer specializing in building secure, scalable, and highly available systems. I work primarily with Python, Django, FastAPI, Docker, PostgreSQL, and modern AI tooling such as PydanticAI, focusing on clean architecture, strong design principles, and reliable DevOps practices. I enjoy solving complex engineering problems and designing systems that are maintainable, resilient, and built to scale. Jared Hasson Jared Hasson Full time lead founding dev at a cyber security saas startup, with 10 yoe and a bachelor's in CS. Building & debugging software products is what I've spent my time on for forever Rudra Bhikadiya Rudra Bhikadiya I build and fix web apps across Next.js, Node.js, and DBs. Comfortable jumping into messy code, broken APIs, and mysterious bugs. If your project works in theory but not in reality, I help close that gap. Omar Faruk Omar Faruk As a Product Engineer at Klasio, I contributed to end-to-end product development, focusing on scalability, performance, and user experience. My work spanned building and refining core features, developing dynamic website templates, integrating secure and reliable payment gateways, and optimizing the overall system architecture. I played a key role in creating a scalable and maintainable platform to support educators and learners globally. I'm enthusiastic about embracing new challenges and making meaningful contributions. Meïr Ankri Meïr Ankri Full-stack developer specializing in React / Next.js / Node.js with 6+ years of experience. I've worked across various sectors including automotive (Reezocar/Société Générale), healthcare (Medical Link SaaS), and e-commerce (Glasman). I build web apps end-to-end, from architecture to production, with a focus on scalability, performance, and code quality. I also mentor junior developers and contribute to technical decisions and code reviews. Kingsley Omage Kingsley Omage Fullstack software engineer passionate about AI Agents, blockchain, LLMs. Taufan Taufan I’m a product-focused engineer and tech leader who builds scalable systems and turns ideas into production-ready platforms. Over the past years, I’ve worked across startups and fast-moving teams, leading backend architecture, improving system reliability, and shipping products used by thousands of users. My strength is not just writing code — but connecting product vision, technical execution, and business impact. PawelPloszaj PawelPloszaj I'm fronted developer with 10+ years of experience with big projects. I have small backend background too

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

Why doesn't jest.mock() work with ES modules?

jest.mock() relies on hoisting the mock setup before require() calls. With ESM, import statements are resolved at parse time before any code runs, so jest.mock() hasn't executed yet when imports are resolved. Use jest.unstable_mockModule() with dynamic import() instead.

Should I switch from Jest to Vitest?

If you're fighting ESM mocking issues, Vitest is worth considering. It has native ESM support, a nearly identical API to Jest, and handles module mocking natively. Migration is usually just changing imports and config, not rewriting tests.

Related Windsurf Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help