Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.
This repository was 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

@jonathanyeongdeliverr

Description

@jonathanyeongdeliverr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions