Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: SyntaxError: Unexpected token ':' at Runtime.loadEsmModule (node_modules/jest-runtime/build/index.js:516:20) #15299

Closed
asimplify opened this issue Sep 11, 2024 · 9 comments

Comments

@asimplify
Copy link

Version

^29.7.0

Steps to reproduce

Can't share a repo because of confidential reasons.

Expected behavior

I expected that test will run.

Actual behavior

ReferenceError: You are trying to import a file after the Jest environment has been torn down. From test/api/agents/index.test.ts.

ReferenceError: You are trying to import a file after the Jest environment has been torn down. From test/api/agents/index.test.ts.

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

SyntaxError: Unexpected token ':'

  at Runtime.loadEsmModule (node_modules/jest-runtime/build/index.js:516:20)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.668 s
Ran all test suites.
(node:6932) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use node --trace-warnings ... to show where the warning was created)

Additional context

import { NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
import { AppNextApiRequest } from "@app/types";
import { getAgents } from "@app/pages/api/agents";
import { respond } from "@app/utils/createa-api-handler";
import { expect, describe, it, jest } from "@jest/globals";

const session = {...}

describe("/api/agents", () => {
  it("should get the list of agents.", async () => {
    const { req, res } = createMocks<AppNextApiRequest, NextApiResponse>({
      session,
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
    });
    await respond(getAgents)(req, res);

    expect(res._getStatusCode).toBe(200);
  }, 6000);
});

jest.config.ts


import type { Config } from "jest";
import nextJest from "next/jest";

const createJestConfig = nextJest({
  dir: "./",
});

const config: Config = {
  verbose: true,
  preset: "ts-jest",
  coverageProvider: "v8",
  testEnvironment: "node",
  moduleDirectories: ["node_modules", "<rootDir>"],
  testPathIgnorePatterns: ["/node_modules/", "/.next/", "/packages/"],
  fakeTimers: {
    enableGlobally: true,
  },
  transform: {
    "^.+\\.(ts|tsx)$": [
      "ts-jest",
      {
        useESM: true,
        isolatedModules: true,
      },
    ],
  },
  extensionsToTreatAsEsm: [".ts", ".tsx"],
  transformIgnorePatterns: ["/emailer/"],
  moduleNameMapper: {
    "^@app/(.*)$": "<rootDir>/$1",
  },
};

export default createJestConfig(config);

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
  Binaries:
    Node: 18.19.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.4.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.4 - ~\AppData\Roaming\npm\pnpm.CMD
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@maxbisesi
Copy link

maxbisesi commented Sep 19, 2024

Hope this helps. I had the exact same issue only it was:

SyntaxError: Unexpected token ';'
at Runtime.loadEsmModule (node_modules/jest-runtime/build/index.js:516:20)

Semi colon instead of a colon. Right before that I was working on another issue and I had changed:

"jest": {
    "transform": {
      "\\.[jt]sx?$": "babel-jest"
    }
  }

to

"jest": {
    "transform": {}
  }

.. though I'm not sure why I did that. I put it back to have babel-jest and ran it again. That error above went away and instead I got a long stack-trace:

SyntaxError: /Users/bisesma/kingfishers-auto/ReduxStore.test.js: Unexpected token, expected "," (32:51)
      30 |         soap: {
      31 |             metadata: {
    > 32 |                 list: jest.fn((q,v) => mocks.at(0));
         |                                                    ^
      33 |             }
      34 |         },
      35 |         restRoot: `none`
      at constructor (node_modules/@babel/parser/src/parse-error.ts:95:45)
      at Parser.toParseError [as raise] (node_modules/@babel/parser/src/tokenizer/index.ts:1496:19)
      at Parser.raise [as unexpected] (node_modules/@babel/parser/src/tokenizer/index.ts:1536:16)
      at Parser.unexpected [as expect] (node_modules/@babel/parser/src/parser/util.ts:157:12)
      at Parser.expect [as parseObjectLike] (node_modules/@babel/parser/src/parser/expression.ts:2066:14)
      at Parser.parseObjectLike [as parseExprAtom] (node_modules/@babel/parser/src/parser/expression.ts:1190:21)
      at Parser.parseExprAtom [as parseExprSubscripts] (node_modules/@babel/parser/src/parser/expression.ts:706:23)
      at Parser.parseExprSubscripts [as parseUpdate] (node_modules/@babel/parser/src/parser/expression.ts:685:21)
      at Parser.parseUpdate [as parseMaybeUnary] (node_modules/@babel/parser/src/parser/expression.ts:647:23)
      at Parser.parseMaybeUnary [as parseMaybeUnaryOrPrivate] (node_modules/@babel/parser/src/parser/expression.ts:388:14)
      at Parser.parseMaybeUnaryOrPrivate [as parseExprOps] (node_modules/@babel/parser/src/parser/expression.ts:400:23)
      at Parser.parseExprOps [as parseMaybeConditional] (node_modules/@babel/parser/src/parser/expression.ts:355:23)
      at Parser.parseMaybeConditional [as parseMaybeAssign] (node_modules/@babel/parser/src/parser/expression.ts:297:21)
      at parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.ts:251:12)
      at Parser.callback [as allowInAnd] (node_modules/@babel/parser/src/parser/expression.ts:3135:12)
      at Parser.allowInAnd [as parseMaybeAssignAllowIn] (node_modules/@babel/parser/src/parser/expression.ts:250:17)
      at Parser.parseMaybeAssignAllowIn [as parseObjectProperty] (node_modules/@babel/parser/src/parser/expression.ts:2305:16)
      at Parser.parseObjectProperty [as parseObjPropValue] (node_modules/@babel/parser/src/parser/expression.ts:2361:12)
      at Parser.parseObjPropValue [as parsePropertyDefinition] (node_modules/@babel/parser/src/parser/expression.ts:2208:17)
      at Parser.parsePropertyDefinition [as parseObjectLike] (node_modules/@babel/parser/src/parser/expression.ts:2080:21)
      at Parser.parseObjectLike [as parseExprAtom] (node_modules/@babel/parser/src/parser/expression.ts:1190:21)
      at Parser.parseExprAtom [as parseExprSubscripts] (node_modules/@babel/parser/src/parser/expression.ts:706:23)
      at Parser.parseExprSubscripts [as parseUpdate] (node_modules/@babel/parser/src/parser/expression.ts:685:21)
      at Parser.parseUpdate [as parseMaybeUnary] (node_modules/@babel/parser/src/parser/expression.ts:647:23)
      at Parser.parseMaybeUnary [as parseMaybeUnaryOrPrivate] (node_modules/@babel/parser/src/parser/expression.ts:388:14)
      at Parser.parseMaybeUnaryOrPrivate [as parseExprOps] (node_modules/@babel/parser/src/parser/expression.ts:400:23)
      at Parser.parseExprOps [as parseMaybeConditional] (node_modules/@babel/parser/src/parser/expression.ts:355:23)
      at Parser.parseMaybeConditional [as parseMaybeAssign] (node_modules/@babel/parser/src/parser/expression.ts:297:21)
      at parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.ts:251:12)
      at Parser.callback [as allowInAnd] (node_modules/@babel/parser/src/parser/expression.ts:3135:12)
      at Parser.allowInAnd [as parseMaybeAssignAllowIn] (node_modules/@babel/parser/src/parser/expression.ts:250:17)
      at Parser.parseMaybeAssignAllowIn [as parseObjectProperty] (node_modules/@babel/parser/src/parser/expression.ts:2305:16)
      at Parser.parseObjectProperty [as parseObjPropValue] (node_modules/@babel/parser/src/parser/expression.ts:2361:12)
      at Parser.parseObjPropValue [as parsePropertyDefinition] (node_modules/@babel/parser/src/parser/expression.ts:2208:17)
      at Parser.parsePropertyDefinition [as parseObjectLike] (node_modules/@babel/parser/src/parser/expression.ts:2080:21)
      at Parser.parseObjectLike [as parseExprAtom] (node_modules/@babel/parser/src/parser/expression.ts:1190:21)
      at Parser.parseExprAtom [as parseExprSubscripts] (node_modules/@babel/parser/src/parser/expression.ts:706:23)
      at Parser.parseExprSubscripts [as parseUpdate] (node_modules/@babel/parser/src/parser/expression.ts:685:21)
      at Parser.parseUpdate [as parseMaybeUnary] (node_modules/@babel/parser/src/parser/expression.ts:647:23)
      at Parser.parseMaybeUnary [as parseMaybeUnaryOrPrivate] (node_modules/@babel/parser/src/parser/expression.ts:388:14)
      at Parser.parseMaybeUnaryOrPrivate [as parseExprOps] (node_modules/@babel/parser/src/parser/expression.ts:400:23)
      at Parser.parseExprOps [as parseMaybeConditional] (node_modules/@babel/parser/src/parser/expression.ts:355:23)
      at Parser.parseMaybeConditional [as parseMaybeAssign] (node_modules/@babel/parser/src/parser/expression.ts:297:21)
      at Parser.parseMaybeAssign [as parseExpressionBase] (node_modules/@babel/parser/src/parser/expression.ts:220:23)
      at parseExpressionBase (node_modules/@babel/parser/src/parser/expression.ts:211:39)
      at Parser.callback [as allowInAnd] (node_modules/@babel/parser/src/parser/expression.ts:3130:16)
      at Parser.allowInAnd [as parseExpression] (node_modules/@babel/parser/src/parser/expression.ts:211:17)
      at Parser.parseExpression [as parseReturnStatement] (node_modules/@babel/parser/src/parser/statement.ts:1067:28)
      at Parser.parseReturnStatement [as parseStatementContent] (node_modules/@babel/parser/src/parser/statement.ts:487:21)
      at Parser.parseStatementContent [as parseStatementLike] (node_modules/@babel/parser/src/parser/statement.ts:426:17)
      at Parser.parseStatementLike [as parseStatementListItem] (node_modules/@babel/parser/src/parser/statement.ts:375:17)
      at Parser.parseStatementListItem [as parseBlockOrModuleBlockBody] (node_modules/@babel/parser/src/parser/statement.ts:1396:16)
      at Parser.parseBlockOrModuleBlockBody [as parseBlockBody] (node_modules/@babel/parser/src/parser/statement.ts:1369:10)
      at Parser.parseBlockBody [as parseBlock] (node_modules/@babel/parser/src/parser/statement.ts:1337:10)
      at Parser.parseBlock [as parseFunctionBody] (node_modules/@babel/parser/src/parser/expression.ts:2589:24)
      at Parser.parseFunctionBody [as parseFunctionBodyAndFinish] (node_modules/@babel/parser/src/parser/expression.ts:2558:10)
      at parseFunctionBodyAndFinish (node_modules/@babel/parser/src/parser/statement.ts:1643:12)
      at Parser.callback [as withSmartMixTopicForbiddingContext] (node_modules/@babel/parser/src/parser/expression.ts:3109:14)
      at Parser.withSmartMixTopicForbiddingContext [as parseFunction] (node_modules/@babel/parser/src/parser/statement.ts:1641:10)
      at Parser.parseFunction [as parseFunctionStatement] (node_modules/@babel/parser/src/parser/statement.ts:1032:17)
      at Parser.parseFunctionStatement [as parseStatementContent] (node_modules/@babel/parser/src/parser/statement.ts:469:21)
      at Parser.parseStatementContent [as parseStatementLike] (node_modules/@babel/parser/src/parser/statement.ts:426:17)
      at Parser.parseStatementLike [as parseModuleItem] (node_modules/@babel/parser/src/parser/statement.ts:363:17)
      at Parser.parseModuleItem [as parseBlockOrModuleBlockBody] (node_modules/@babel/parser/src/parser/statement.ts:1395:16)
      at Parser.parseBlockOrModuleBlockBody [as parseBlockBody] (node_modules/@babel/parser/src/parser/statement.ts:1369:10)
      at Parser.parseBlockBody [as parseProgram] (node_modules/@babel/parser/src/parser/statement.ts:217:10)
      at Parser.parseProgram [as parseTopLevel] (node_modules/@babel/parser/src/parser/statement.ts:199:25)
      at Parser.parseTopLevel [as parse] (node_modules/@babel/parser/src/parser/index.ts:46:10)
      at parse (node_modules/@babel/parser/src/index.ts:65:38)
      at parser (node_modules/@babel/core/src/parser/index.ts:28:19)
          at parser.next (<anonymous>)
      at normalizeFile (node_modules/@babel/core/src/transformation/normalize-file.ts:50:24)
          at normalizeFile.next (<anonymous>)
      at run (node_modules/@babel/core/src/transformation/index.ts:39:36)
          at run.next (<anonymous>)
      at transform (node_modules/@babel/core/src/transform.ts:29:20)
          at transform.next (<anonymous>)
      at step (node_modules/gensync/index.js:261:32)
      at node_modules/gensync/index.js:273:13
      at async.call.result.err.err (node_modules/gensync/index.js:223:11)
      at node_modules/gensync/index.js:189:28
      at cb (node_modules/@babel/core/src/gensync-utils/async.ts:90:7)
      at node_modules/gensync/index.js:113:33
      at step (node_modules/gensync/index.js:287:14)
      at node_modules/gensync/index.js:273:13
      at async.call.result.err.err (node_modules/gensync/index.js:223:11)

I removed that semi colon from the end of list: jest.fn((q,v) => mocks.at(0)) and boom, the problem went away.

@muhammadfaizan027915
Copy link

@maxbisesi

I think you may added ; at the place of : but In my case I was getting error related to : in jest-runtime/build.

The same code I had run with Vitest it was working with Vitest. I don't know why it wasn't working with Jest.

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 19, 2024
@muhammadfaizan027915
Copy link

@znarf

What could be way to avoid this error ?

@github-actions github-actions bot removed the Stale label Oct 20, 2024
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Nov 19, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

1 similar comment
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants