Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

swc/jest fails tests with certain functions imported by namespace imports #168

Open
jonathanyeongdeliverr opened this issue Dec 22, 2023 · 0 comments

Comments

@jonathanyeongdeliverr
Copy link

jonathanyeongdeliverr commented Dec 22, 2023

Sandbox: https://codesandbox.io/p/devbox/pt3dqy

Let's say I have a file (findGroupings.ts):

import * as _ from "lodash";

export function findGrouping(fruits: string[]) {
  const groups = _(fruits).groupBy(
    (fruitName) => `FRUIT_${fruitName.substring(0, 1)}`,
  );
  return groups.value();
}

And I have a test (findGroupings.uspec.ts):

import { findGrouping } from "./findGrouping";

describe("findGrouping", () => {
  it("returns a grouping", () => {
    const group = findGrouping(["apple", "banana", "aorange"]);
    expect(group).toMatchObject({
      FRUIT_a: ["apple", "aorange"],
      FRUIT_b: ["banana"],
    });
  });
});

When I run the test with @swc/jest as the transform. I get an error TypeError: _lodash is not a function. But when I run it with ts-jest as the transform I don't get any error and the test passes.

Here's my .swcrc

{
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "tsx": false,
        "decorators": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true
      },
      "target": "es2022"
    },
    "module": {
      "type": "commonjs"
    }
  }

Here's my tsconfig

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2022",
    "lib": ["es2022", "dom"],
    "noImplicitAny": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "allowJs": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "strict": true,
    "strictNullChecks": true,
    "isolatedModules": true
  }
}

Am I doing something wrong? Or is this a known issue? I don't know why the tests passes with ts-jest but not @swc/jest

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant