Documentación oficial sobre Jest
- Instalaciones de desarrollo (super test es útil para probar Express)
npm install -D jest @types/jest ts-jest supertest ts-node
- Crear archivo de configuración de Jest
npx jest --init
# si no funciona el anterior
npm init jest@latest
- 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'],
- Crear scripts en el package.json
"test": "jest",
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage",
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