Skip to content

Instantly share code, notes, and snippets.

@elkake
Forked from Klerith/pasos-node-ts-jest.md
Last active September 21, 2025 13:40
Show Gist options
  • Select an option

  • Save elkake/3f7b8e7fe2de3c302827324649b7c223 to your computer and use it in GitHub Desktop.

Select an option

Save elkake/3f7b8e7fe2de3c302827324649b7c223 to your computer and use it in GitHub Desktop.
Note + TypeScript + Jest = Testing

Pasos para configurar Jest con TypeScript, en Node

Documentación oficial sobre Jest

  1. Instalaciones de desarrollo (super test es útil para probar Express)
npm install -D jest @types/jest ts-jest supertest ts-node
  1. Crear archivo de configuración de Jest
npx jest --init  
# si no funciona el anterior
npm init jest@latest
  1. En el archivo jest.config.js configurar
preset: 'ts-jest',
testEnvironment: "jest-environment-node",

// Opcional - The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: ['dotenv/config'],
  1. Crear scripts en el package.json
"test": "jest",
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage",
@elkake
Copy link
Author

elkake commented Sep 21, 2025

Error: Jest: Failed to parse the TypeScript config file C:\Users\giake\Desktop\CursosProgramacion\node_FerHerrera\seccion6_testing\jest.config.ts
TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:201:1 - error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.

201 export default config

La forma de eliminar este error al momento de usar jest en ts, es agregar el parametro
"moduleResolution": "nodenext",
en el tsconfig.json

@elkake
Copy link
Author

elkake commented Sep 21, 2025

Lineas completas:
"module": "nodenext", //Define cómo TypeScript genera el JavaScript de salida.
"moduleResolution": "nodenext", // Define cómo TypeScript resuelve los imports cuando vos escribís import ... from "algo".
"target": "esnext", //Define que version de js usa el compilado
"types": ["node", "jest"],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment