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

662
Views
¿Cómo hacer que ts-jest funcione con la sintaxis de importación/exportación de los archivos js que se importan en mis importaciones?

jest no puede importar la importación de mi importación, lo que hace que el comando de npm run test falle con SyntaxError: Unexpected token 'export' en la primera línea de mi archivo bar.ts Para este ejemplo foo.js es un archivo local, no un módulo de nodo. Puedo cambiar foo.js a foo.ts y cambiar la importación en bar.ts pero esa no es la solución.

Archivos en mi carpeta src :

  • foo.js :
 export const magicNumber = 42;
  • bar.ts :
 import { magicNumber } from './foo.js'; export const secondMagicNumber = magicNumber / 2;
  • bar.test.ts :
 import { secondMagicNumber } from './bar'; import assert from 'assert'; it('should work', function() { assert.strictEqual(secondMagicNumber, 21); });

Archivos en la carpeta raíz :

  • jest.config.js :
 export default { preset: 'ts-jest', testEnvironment: 'node', };
  • package.json :
 { "name": "testing-with-jest", "version": "1.0.0", "description": "", "scripts": { "test": "jest" }, "author": "", "license": "ISC", "devDependencies": { "@types/jest": "^26.0.20", "jest": "^26.6.3", "ts-jest": "^26.5.2", "typescript": "^4.2.2" }, "type": "module" }
  • tsconfig.json :
 { "compilerOptions": { "target": "ESNext", "module": "ESNext", "outDir": "./dist", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "declaration": true, "moduleResolution": "node" }, "include": ["./src/**/*.ts"] }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Entonces, el problema es que tanto ts-jest como jest son difíciles de configurar para la sintaxis de importación/exportación de ES6. Hice exactamente eso por:

  1. Instalación de las versiones jest y ts-jest >= 27.0.0.
  2. Usando esta configuración mínima:

tsconfig.json :

 { "compilerOptions": { "esModuleInterop": true, "allowJs": true } }

jest.config.ts :

 export default { globals: { extensionsToTreatAsEsm: ['.ts', '.js'], 'ts-jest': { useESM: true } }, preset: 'ts-jest/presets/js-with-ts-esm', // from https://stackoverflow.com/a/57916712/15076557 transformIgnorePatterns: [ 'node_modules/(?!(module-that-needs-to-be-transformed)/)' ] }
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!