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

334
Views
jest trying to transform a file in node_modules (worked in a previous version)

I had jest v27.2.4 and ts-jest v27.0.5 and tests worked on, on the latest versions (I think they are 27.4.x) tests don't run anymore since jest complains about a .js file inside node_modules

I tried to downgrade by setting the specific version in package.json (i.e. without the ^) but since jest itself has dependencies on other libraries with ^ it is still upgrading them

The error when running the tests:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    By default "node_modules" folder is ignored by transformers.

    Details:

    /home/me/code/node_modules/@middy/core/index.js:2
      plugin?.beforePrefetch?.()
             ^

    SyntaxError: Unexpected token '.'

      1 | import { logger } from "@libs/logger";
    > 2 | import middy from "@middy/core";
        | ^
      at Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (../../libs/middleware/src/index.ts:2:1)

my jest.config.js file:

module.exports = {
  //clearMocks: true,
  //coverageDirectory: "coverage",
  //collectCoverage: true,
  verbose: true,
  globals: {
    "ts-jest": {
      tsconfig: "tsconfig.json",
    },
  },
  modulePathIgnorePatterns: ["dist"],
  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
  },
  testEnvironment: "node",
  testMatch: ["**/__tests__/*.+(ts|tsx|js)", "**/*.spec.+(ts|tsx|js)"],
};

my tsconfig.json file:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 14",
  "compilerOptions": {
    "incremental": true,
    "target": "ES2019",
    "module": "commonjs",
    "lib": [
      "ES2019"
    ],
    "declaration": true,
    "sourceMap": true,
    "composite": false,
    "removeComments": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "moduleResolution": "node",
    "types": [
      "node"
    ],
    "esModuleInterop": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true
  }
}

Any clues how to fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I had the same exact error message, though not in using jest but hopefully this fixes it for you too. Since both yours and mine highlighted Middy as the source of the error - i.e. from yours

    /home/me/code/node_modules/@middy/core/index.js:2
      plugin?.beforePrefetch?.()
             ^

    SyntaxError: Unexpected token '.'
             ^

and mine:

/.webpack/service/src/functions/functionName/handler.js:78292
  plugin?.beforePrefetch?.()
         ^

SyntaxError: Unexpected token '.'

To me this it seemed odd that there was optional chaining (object?.property), as this should be handled by the compiler before getting to a .js file (in Node 12 at least - it's different if you're using Node 14 and above).

Checking middy's most recent releases shows an upgrade to 2.5.7 yesterday, with the release message describing a recent transpiling error in 2.5.6 which is resolved by upgrade to 2.5.7. Upgrading my middy dependencies to 2.5.7 indeed eliminated this error for me, i.e. I no longer get an error when trying to call my code (in AWS Lambda for what it's worth).

This is what my dependencies in my package.json now look like:

...
"dependencies": {
    "@middy/core": "^2.5.7",
    "@middy/http-error-handler": "^2.5.7",
    "@middy/http-event-normalizer": "^2.5.7",
    "@middy/http-json-body-parser": "^2.5.7",
    "@middy/util": "^2.5.7"
  },
...

Whereas before they were:

...
"dependencies": {
    "@middy/core": "^2.5.3",
    "@middy/http-error-handler": "^2.5.4",
    "@middy/http-event-normalizer": "^2.5.4",
    "@middy/http-json-body-parser": "^2.5.4",
    "@middy/util": "^2.5.3"
  },
...

Where my package-lock.json had two of the middy packages on 2.5.6, which as mentioned above will have been the cause of the issue.

If middy isn't a dependency in your package.json and is a transitive dependency of something else, maybe try editing the middy version number in the package-lock.json itself to 2.5.7?

about 4 years ago · Juan Pablo Isaza 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!