typescriptjestjsts-jestcommonjsesmodules

SyntaxError: Unexpected token 'export' in Jest


I am using Node v22.12.0 with Jest. I am trying to import a library that works find on my other typescript projects (with NestJS) and I get the following error.

SyntaxError: Unexpected token 'export'

      13 | import { Stringifier, stringify as csvStringify } from 'csv-stringify';
      14 | import * as path from 'path';
    > 15 | import * as recase from 'change-case';
         | ^

Is there a configuration that I'm missing?


Solution

  • The issue that package change-case has only ESM version (lates version code) and Jest has only experimental ESM support feature. There is a common approach to fix such issue. Modify field transformIgnorePatterns in your jest config to allow babel/whatever transforming for change-case node module as well(docs):

     transformIgnorePatterns: ['/node_modules/(?!change-case/)']
    

    By default Jest excludes all node_modules change-case. So we should include change-case into transformation explicitly.