Jest Unexpected Token in TypeScript

This is one method of resolving unexpected tokens in TypeScript.

The Error

I ran into this recently when upgrading some libraries, including uuid, specifically. I ran tests, and they failed. The error is big, but it's pretty helpful and descriptive. Here was the error:


  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see for how to enable it.
     • If you are trying to use TypeScript, see
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    For information about custom transformations, see:


    ({"Object.":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';

    SyntaxError: Unexpected token 'export'

    > 1 | import { v4 as uuid } from "uuid";
        | ^

There are several tips on what might be wrong and recommendations on how to resolve it.

Because the uuid library I upgraded now did not pre-compile its ES modules, I had to transpile it. This is the recommended solution in bullet number 3.

Default is no compilation

The default configuration for Jest and TypeScript is to avoid compilation of node_modules. This is because npm packages are traditionally shipped precompiled. But this is becoming less of a sure thing.

Now, when 1) tooling to compile is so widespread in project toolchains and 2) ES modules are becoming native on the latest platforms, the tradeoffs have shifted, and it's useful to ship the original ES modules in some cases.

Adjust Transformation Configuration

Transpilation/transformation needs to be reconfigured to override the default.

Adjust jest.config.js to ignore all node_modules except the few you specify. Here, we specify to not ignore (read, transpile) /node_modules/uuid/:

transformIgnorePatterns: [

This is implemented as a regex lookaround that reads as "match on all /node_modules/ except /node_modules/uuid/".

Save this config and re-run your tests, and you will no longer get the unexpected token message from Jest.