Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.4K
Views
Jest and Typescript: Cannot use import statement outside a module

Im trying to run unit tests in a monorepo where I have configured Jest and Typescript as below:

Jest:

module.exports = {
  displayName: 'xplat-web-features',
  preset: '../../../../jest.preset.js',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+\\.[tj]sx?$': 'ts-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../../../coverage/libs/xplat/web/features',
};

TSConfig:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../../../dist/out-tsc",
    "module": "commonjs",
    "types": ["jest", "node"]
  },
  "include": [
    "**/*.spec.ts",
    "**/*.spec.tsx",
    "**/*.spec.js",
    "**/*.spec.jsx",
    "**/*.d.ts"
  ]
}

And when I run Jest with the nx command, I get SyntaxError: Cannot use import statement outside a module

I have tried the following with no luck

  • Set babel-js as the transpiler in Jest config
  • Set "module" to es6 in compilerOptions of TS config
  • Run Jest with NODE_OPTIONS=--experimental-vm-modules

Basically hitting a wall at this point and don't know where to go from here.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Add this to your jest config file:

 "transform": {
     "^.+\\.ts?$": "ts-jest"
 }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!