Error in TypeScript/jest combination

Asked 1 years ago, Updated 1 years ago, 413 views

We are building an environment to test TypeScript alone with jest, but we do not know what to do with the following error.
ttests/index.test.ts は is empty.

FAIL tests/index.test.ts
  ● Test suite failed to run

    /home/tatuto/Node-RED/jest-test/node_modules/jest-environment-node/build/index.js:176
        consttimerRefToId=timer=>timer?.id;
                                            ^

    SyntaxError: Unexpected token '.'

      102 |       }
      103 | 
    >104 | oldLoader (mod, filename);
          |       ^
      105 |     };
      106 |   });
      107 | return function revert() {

      at Object.newLoader [as.js] (../../../usr/share/nodejs/pirates/lib/index.js: 104:7)
      at ScriptTransformer.requireAndTransfileModule(../../usr/share/nodejs/@jest/transform/build/ScriptTransformer.js:689:66)

Test Suites: 1 failed, 1 total
Tests—0 total
Snapshots: 0 total
Time—2.12s

package.json

{
  "name": "jest-test",
  "version": "1.0.0",
  "description":",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "keywords": [ ],
  "author":",
  "license": "ISC",
  "devDependencies": {
    "@types/jest": "^29.4.0",
    "jest": "^29.4.3",
    "ts-jest": "^29.0.5",
    "typescript": "^4.9.5"
  }
}

tconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    " strict"—true,
    "skipLibCheck"—true
  }
}

jest.config.js

/**@type{import('ts-jest').JestConfigWithTsJest}*/
module.exports={
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ["<rootDir>/tests", "<rootDir>/src"],
  collectCoverage:true,
  collectCoverageFrom:[
    "**/*.ts",
    "!**/node_modules/**",
  ],
  coverageDirectory: 'coverage_dir',
  coverageReporters: ["html"]
};

typescript jestjs

2023-03-03 16:50

1 Answers

Please upgrade the Node.js version

The code timer?id that is being questioned is a relatively new syntax: optional chaining(?).

Node.js has supported this since v14 released in 2020 (although it was already 3 years ago).In other words, update to or later.

According to the comment, the Node.js version in your environment is v12.22.9, but v12 is an older version that is no longer supported.Also, v14 will expire in April this year and v16 in September.

Note: Node.js release schedule

Therefore, If there is no particular problem, we recommend updating to the current LTS version of v18 series.However, be aware that some packages may not be compatible with the OpenSSL upgrade.


2023-03-03 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.