jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.06k stars 6.44k forks source link

[Bug]: Calling ts.createSourceFile in a test file hangs with Typescript 5.x.x #15195

Open asnaseer opened 2 months ago

asnaseer commented 2 months ago

Version

29.7.0

Steps to reproduce

I have an Expo 50 React Native app and have some code that uses the Typescript createSourceFile method. If I call this method from a jest test then the test seems to hang for ever. Calling it directly via a Typescript script works just fine.

Also, if I downgrade to Typescript 4.9.5 then the jest test passes.

These are the relevant packages I used:

  "devDependencies": {
    "@types/jest": "^29.5.12",
    "jest-extended": "^4.0.2",
    "ts-jest": "^29.2.2",
    "typescript": "^5.5.3"
  },

Here is a minimal jest test to reproduce the error (in file src/broken.spec.ts):

import ts from "typescript";

describe("broken", () => {
  const createSource = (sourceCode: string): ts.SourceFile => {
    const sourceFile = ts.createSourceFile(
      "test.ts",
      sourceCode,
      ts.ScriptTarget.Latest,
      true
    );

    return sourceFile;
  };

  it("should create Typescript source file", () => {
    expect(
      createSource(
        `
        const doIt = (): void => {
          console.log("hello");
        };
      `
      )
    ).toBeDefined();
  });
});

I use yarn 1.22.10 to run the test.

Here is a minimal Typescript script that works just fine when run with npx ts-node src/broken.ts:

import ts from "typescript";

const createSource = (sourceCode: string): ts.SourceFile => {
  const sourceFile = ts.createSourceFile(
    "test.ts",
    sourceCode,
    ts.ScriptTarget.Latest,
    true
  );

  return sourceFile;
};

createSource(
  `
    const doIt = (): void => {
      console.log("hello");
    };
  `
);

Expected behavior

The test should not hang

Actual behavior

The test hangs

Additional context

No response

Environment

System:
    OS: macOS 13.6.7
    CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  Binaries:
    Node: 18.20.2 - ~/.nvm/versions/node/v18.20.2/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.2/bin/npm
    pnpm: 8.6.2 - ~/Library/pnpm/store/v3/pnpm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
github-actions[bot] commented 1 month ago

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.

asnaseer commented 3 weeks ago

I am still looking for a solution to this please

asnaseer commented 1 week ago

I just tried the same scenarios with these updated libs and still get the same error:

    "@types/jest": "^29.7.0",
    "jest-extended": "^4.0.2",
    "ts-jest": "^29.2.5",
    "typescript": "^5.6.2"