jest icon indicating copy to clipboard operation
jest copied to clipboard

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

Open asimplify opened this issue 5 months ago • 4 comments

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

asimplify avatar Sep 11 '24 07:09 asimplify