Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

714
Visualizações
Importing pure ESM module in TS project fails Jest test with import error

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have to tell Jest to transform those ESM bundled libraries with the transformIgnorePatterns option.

This options specifies the ignored paths and it defaults to ["/node_modules/", "\\.pnp\\.[^\\\/]+$"] which explains why the libraries are not being transformed.

The most straightforward solution is to set an empty array:

transformIgnorePatterns: []

But you could also set a more fine-grained config to exclude everything except some particular libraries, like in the regex example of the docs.

Also, add the "allowJs": true option in the TypeScript configuration.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda