Sitemap

Member-only story

Jest encountered an unexpected token

2 min readMar 27, 2025

Hey fellow developers, if you’re reading this, you’re probably getting this juicy error when running your unit tests with Jest: Jest encountered an unexpected token. You probably asked ChatGPT, tried the solution, and realized it was bullshit.

Jest encountered an unexpected token

This usually means that in your module which you are writing a unit test for, you are using something, perhaps a method from a external dependency, and that dependency is using ES Module and Jest does not transpile code from node_modules. In above error within the Details section, you can see which package(s) having this problem.

Jest encountered an unexpected token

let’s say the package that does have this problem and you see in the above error within the Details is called: @oldbutawesome/tony and you are importing something in your module from it, let’s say a method called whoIsAwesome()

The best and perhaps the easiest way to fix this problem, is to mock it

jest.mock('@oldbutawesome/tony', () => ({
whoIsAwesome: jest.fn(),
}))

Simple as that, but if you don’t want to mock it for whatever reasons, there’s one other thing you can do, to update your test script in your package json to contain the following:

--

--

Emad Dehnavi
Emad Dehnavi

Written by Emad Dehnavi

With 8 years as a software engineer, I write about AI and technology in a simple way. My goal is to make these topics easy and interesting for everyone.

No responses yet