npm - No se puede encontrar el nombre 'it' en Jest TypeScript
Intento crear una configuración inicial para Jest en React + TypeScript. He completado la configuración inicial y trato de comprobar si la prueba se ejecuta. Cuando ejecuto la prueba usando el comando npm test, obtengo el siguiente error:
Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
He instalado los tipos para Jest así como eliminado los tipos en tsconfig.json, pero todavía sigo recibiendo el mismo error.
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"plugins": [{ "name": "typescript-tslint-plugin" }],
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"pretty": true,
"baseUrl": "src",
"types": ["jest"],
"typeRoots": ["./src/types"],
"suppressImplicitAnyIndexErrors": true
},
"include": ["src", "node_modules/@types/jest"],
"exclude": ["node_modules"]
}
`
Package.json
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
},
"devDependencies": {
"@babel/plugin-proposal-export-default-from": "^7.2.0",
"@types/enzyme": "^3.9.3",
"@types/jest": "^24.0.14",
"enzyme": "^3.10.0",
"gh-pages": "^1.2.0",
"husky": "^2.2.0",
"jest": "^24.8.0",
"node-sass": "^4.11.0",
"prettier": "^1.17.0",
"react-scripts": "2.1.8",
"react-test-renderer": "^16.8.6",
"stylelint": "^9.3.0",
"stylelint-config-recommended-scss": "^3.2.0",
"stylelint-config-standard": "^18.2.0",
"stylelint-order": "^0.8.1",
"stylelint-scss": "^3.1.3",
"ts-jest": "^24.0.2",
"tslint": "^5.16.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"tslint-react": "^4.0.0",
"tslint-react-hooks": "^2.1.0"
}
Respuesta #1
Instalar
npm install -D jest @types/jest ts-jest
jest.config.js -- en la raíz
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
}
tsconfig.json
{
"compilerOptions": {
...
"types": ["reflect-metadata", "jest"],
"typeRoots": ["./types", "./node_modules/@types"]
...
},
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"],
"include": ["./src/**/*.tsx", "./src/**/*.ts"]
}
Respuesta #2
Importa esto en cualquiera de tus archivos de prueba:
import '@types/jest';
Respuesta #3
Tuve que agregar "@types/jest" a mi array "types" en mi tsconfig.json.
Respuesta #4
Para mí, el problema era que mi tsconfig.json estaba construido con
"include": [
"src"
]
Tuve que cambiar eso a
"include": [
"src",
"tests"
]
(Todas mis pruebas están en el directorio 'tests')
Respuesta #5
En tsconfig.json agrega el siguiente código
"types": ["jest"],
"typeRoots": ["./src/types", "node_modules/@types"],
Respuesta #6
He probado todas las soluciones mencionadas, pero desafortunadamente, ninguna funcionó para mí. La solución que funcionó en mi caso fue referenciar las definiciones de tipo mediante /// <reference types="@types/jest" />; al principio del archivo. Primero necesitas instalar el paquete @types/jest. La desventaja es que tienes que importarlo en todos los archivos.
