You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Turborepo+Jest无法转译外部ESM包问题求助

问题

无法转译采用ESM语法导出的外部包

Jest版本

29.5.0

复现步骤

  • 克隆仓库
  • 安装依赖:yarn workspace jest-issue install
  • 运行测试:yarn workspace jest-issue test example

预期行为

测试能够编译、运行并通过

实际行为

Jest遇到意外token错误,详情如下:

Jest encountered an unexpected token
    /Users/.../turborepo_jest_issue/node_modules/oauth4webapi/build/index.js:7
    export const clockSkew = Symbol();
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import * as o from "oauth4webapi";
        | ^
      2 |
      3 | export const value = o.skipStateCheck;

补充说明

  • .babelrc中的@babel/plugin-transform-modules-commonjs无法转换外部包构建文件中的ESM导出
  • jest.config.ts中的transformIgnorePatterns无法实现对该外部包的转译

环境信息

System:
    OS: macOS 13.4
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.5.0 - /usr/local/bin/npm

jest.config.ts

import type {Config} from 'jest';
import path from 'path';

const config: Config = {
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.(js|jsx)$': 'babel-jest',
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  "transformIgnorePatterns": [
    `${path.join(process.cwd(), "../..")}/node_modules/(?!(oauth4webapi))`,
  ],
};

export default config;

.babelrc

{
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
  }
}

example.ts

import * as o from "oauth4webapi";

export const value = o.skipStateCheck;

example.spec.ts

import {value} from "./example";

describe("example", () => {
  it("should be true", () => {
    expect(value).toBe(true);
  });
});

内容的提问来源于stack exchange,提问作者wongx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 05:25:19