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

190
Views
Jest no aceptará esperas de nivel superior con NodeJS16 y TypeScript

Estoy tratando de actualizar mi aplicación NodeJS12 & TypeScript a Node16, uno si el motivo es la necesidad de usar esperas de nivel superior.

El código se compila correctamente después de la actualización, pero Jest no aceptará el código de espera de nivel superior específico:

 ts-jest[ts-compiler] (WARN) src/xxx.ts:11:17 - error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher. 11 const project = await client.getProjectId(); ~~~~~ FAIL src/xxx.test.ts ● Test suite failed to run Jest encountered an unexpected token

paquete.json:

 { "name": "functions", "scripts": { "lint": "eslint --ext .js,.ts .", "lint:fix": "eslint --ext .js,.ts . --fix", "build": "tsc -b", "build:watch": "tsc-watch", "serve": "...", "test": "env-cmd -f .env.json jest --runInBand --verbose" }, "type": "module", "engines": { "node": "16" }, "main": "lib/index.js", "exports": "./lib/index.js", "dependencies": { "test": "^1.0.0" }, "devDependencies": { "@google-cloud/functions-framework": "^2.1.0", "@types/busboy": "^1.3.0", "@types/compression": "1.7.2", "@types/cors": "^2.8.12", "@types/express": "^4.17.12", "@types/express-serve-static-core": "^4.17.28", "@types/google-libphonenumber": "^7.4.23", "@types/jest": "^27.4.0", "@types/jsonwebtoken": "^8.5.7", "@types/luxon": "^2.0.9", "@types/node-zendesk": "^2.0.6", "@types/sinon": "^10.0.6", "@types/supertest": "^2.0.11", "@types/swagger-ui-express": "^4.1.3", "@types/uuid": "^8.3.4", "@types/yamljs": "^0.2.31", "@typescript-eslint/eslint-plugin": "^5.9.0", "@typescript-eslint/parser": "^5.9.0", "env-cmd": "^10.1.0", "eslint": "^8.6.0", "eslint-config-google": "^0.14.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.25.4", "eslint-plugin-prettier": "^4.0.0", "eslint-plugin-react-hooks": "^4.2.1-beta-a65ceef37-20211130", "jest": "^27.4.7", "prettier": "^2.5.1", "sinon": "^12.0.1", "supertest": "^6.2.1", "ts-jest": "^27.1.3", "ts-node": "^10.4.0", "tsc-watch": "^4.6.0", "typescript": "^4.5.4" }, "private": true }

tsconfig.json:

 { "compilerOptions": { "module": "es2022", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": false, "strict": true, "target": "es2021", "moduleResolution": "Node", "resolveJsonModule": true }, "compileOnSave": true, "include": [ "src" ], "ts-node": { "moduleTypes": { "jest.config.ts": "cjs" } } }

jest.config.ts:

 export default { roots: [ '<rootDir>/src' ], setupFiles: ['./src/setupJestEnv.ts'], preset: 'ts-jest', testEnvironment: 'node', testPathIgnorePatterns: ['/node_modules/'], coverageDirectory: './coverage', coveragePathIgnorePatterns: ['node_modules', 'src/database', 'src/test', 'src/types'], globals: { 'ts-jest': { diagnostics: false } }, };

Realmente no puedo entender lo que está mal aquí. ¿Alguna idea?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Últimamente se me ocurrió que, que yo sepa, CJS no es compatible con la espera de nivel superior, lo que significa que debe usar ts-jest con esm.

mi jest.config.json

 { "extensionsToTreatAsEsm": [".ts"], "globals": { "ts-jest": { "useESM": true } }, "preset": "ts-jest/presets/default-esm", "moduleFileExtensions": ["js", "json", "ts"], "rootDir": ".", "testEnvironment": "node", "testRegex": "spec.ts$", "modulePathIgnorePatterns": ["<rootDir>/dist/", "<rootDir>/node_modules/"], "moduleNameMapper": { "^src/(.*)": "<rootDir>/src/$1" }, "collectCoverage": true, "coverageDirectory": "./coverage", "collectCoverageFrom": ["src/**/*.(t|j)s"], "coveragePathIgnorePatterns": [ ".module.ts$", ".spec.ts$", "src/database/", "src/server.ts" ], "verbose": true }

También deberá ejecutar jest con la opción --experimental-vm-modules

 node --experimental-vm-modules -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand --config jest.overall.json
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!