Skip to content

Instantly share code, notes, and snippets.

@Klerith
Created August 19, 2023 18:35
Show Gist options
  • Select an option

  • Save Klerith/98d7b1bc0f1525e892f260813cad1007 to your computer and use it in GitHub Desktop.

Select an option

Save Klerith/98d7b1bc0f1525e892f260813cad1007 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
  1. Crear archivo de configuración de Jest
npx jest --init
  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 --watch",
"test:coverage": "jest --coverage",
@riosbrian
Copy link

Si usan aliases, sigan la siguiente configuración:

tsconfig.json

"resolveJsonModule": true,
"esModuleInterop": true

jest.config.ts

import type { Config } from "jest";
import { pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "./tsconfig.json";

const config: Config = {
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: "<rootDir>/",
  }),
// resto de la configuración de Fernando
}

Esto es porque en mi caso no uso como alias '@src', me gusta crear varios, por ejemplo '@modules' o '@config' dentro del source.

@carlos-suarez-payu-gpo
Copy link

El paso 2. se actualizo a este comando.

npm init jest@latest

Tomado de https://jestjs.io/docs/getting-started#generate-a-basic-configuration-file

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