question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Version

27.1.1

Steps to reproduce

Trying to use the file-type module which is pure ESM in a TS project but my jest fails. I have set the ESM options as indicated here but still get a SyntaxError: Cannot use import statement outside a module error.

I have made a sandbox here.

Summary of my code:

import { fileTypeFromBuffer } from "file-type";

Jest config:

export default {
  testEnvironment: "jest-environment-node",
  globals: {
    extensionsToTreatAsEsm: [".ts"],
    "ts-jest": {
      useESM: true,
    },
  },
  transform: {
    "^.+\\.(ts|tsx|js|jsx)?$": "ts-jest",
    //"^.+\\.tsx?$": "ts-jest",
  },
  moduleNameMapper: {
    "^(\\.{1,2}/.*)\\.js$": "$1",
  },
  preset: "ts-jest",
  //preset: 'ts-jest/presets/default-esm' // or other ESM presets
};

TSConfig:

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "target": "ES2018",
    "module": "commonjs",
    "lib": ["es2018"],
    "declaration": true,
    "strict": true,
    "strictNullChecks": true,
    "alwaysStrict": true,
    //importsNotUsedAsValues
    // "noImplicitAny": false,
    "noImplicitReturns": false,
    "noImplicitThis": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noEmit": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    // "typeRoots": ["./node_modules/@types"]
    "resolveJsonModule": true,
    "outDir": "dist",
    "baseUrl": ".",

    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

Any ideas?

Expected behavior

Expected to run without errors

Actual behavior

SyntaxError: Cannot use import statement outside a module

Debug log

https://gist.github.com/b5b24ed3e4fd2e9fb0298e01f1b41287

Additional context

No response

Environment

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i7-1060NG7 CPU @ 1.20GHz
  Binaries:
    Node: 16.3.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.15.1 - /usr/local/bin/npm
  npmPackages:
    jest: ^27.4.4 => 27.4.4 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
ahnpnlcommented, Jan 2, 2022

Depending on which mode you want to run Jest: either CJS or ESM.

If you want to run in CJS mode, you would need to inform Jest to transform file-type to CJS via transformIgnoredPatterns

If you want to run in ESM mode, you would need to follow ts-jest ESM doc. It seems like you are trying to run Jest in ESM mode but you probably miss a few things:

  • module in tsconfig must be ESM value like: ES2015 or ES2020 etc…
  • use preset ts-jest/presets/defaults-esm

When I set these 2 on, the test will throw a different error, but it’s no longer Jest errors.

I hope this helps.

0reactions
marwejcommented, Jan 7, 2022

I’m having the same issue and would be happy to know about any ready solution to this (ts-jest-resolver didn’t do it).

Edit: https://www.npmjs.com/package/jest-node-exports-resolver fixed it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug Definition & Meaning - Merriam-Webster
noun (1) · 1. a · 2. : an unexpected defect, fault, flaw, or imperfection · 3. a · 4. : a concealed...
Read more >
Bug (2006) - IMDb
An unhinged war veteran holes up with a lonely woman in a spooky Oklahoma motel room. The line between reality and delusion is...
Read more >
bug - Wiktionary
(entomology) An insect of the order Hemiptera (the “true bugs”). Any of various species of marine or freshwater crustaceans; e.g. a Moreton Bay...
Read more >
Bug - Wikipedia
A terrestrial arthropod animal (with at least six legs). Insect, a six-legged arthropod · Covert listening device, used in surveillance, espionage and policing ......
Read more >
BUG | definition in the Cambridge English Dictionary
bug noun (INSECT) ... an insect: Some tiny white bugs had eaten the leaves of my house plants. ... A bug is also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found