Jest SyntaxError: Unexpected token 'export' in TypeScript project

72 Views Asked by At

I'm encountering an issue while running Jest in my TypeScript project. I have a TypeScript file located at rootDir/src/_services/index.ts, and Jest is throwing the following error:

    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './auth.service';                                                                                   ^^^^^^

    SyntaxError: Unexpected token 'export'

The content of index.ts includes the statement export * from './auth.service';, and it seems that Jest is having trouble parsing it. export * from './auth.service'

I'm using a custom Jest configuration along with ts-jest. Here is a snippet from my Jest configuration file

const createJestConfig = nextJest({
  dir: './',
});

const customJestConfig = {
  // ... other configurations ...
  transformIgnorePatterns: [
    '/node_modules/',
    '/@bufbuild/protobuf/',
    '/src/_services/index\\.ts$', // Ignore this path
  ],
  // ... other configurations ...
};
module.exports = createJestConfig(customJestConfig);```
1

There are 1 best solutions below

0
Andrey Smolko On

Is that a reason why you included the third element ('/src/_services/index\.ts$') here?:

 transformIgnorePatterns: [
    '/node_modules/',
    '/@bufbuild/protobuf/',
    '/src/_services/index\\.ts$', // delete this!
  ],

coz it means that your file /src/_services/index.ts will NOT be transformed by any tool and keep its ts syntax.