Semver LRU 不是纱线工作区中的构造函数

问题描述 投票:0回答:1

我有一个项目,已从独立的 npm 包迁移到工作区中的纱线包。我的配置在构建和类型检查方面工作正常,但是当我尝试运行

yarn test
时,我的测试套件失败并显示

    TypeError: LRU is not a constructor

      at Object.<anonymous> (../node_modules/semver/classes/range.js:202:15)
      at Object.<anonymous> (../node_modules/semver/classes/comparator.js:141:15)
      at Object.<anonymous> (../node_modules/semver/index.js:29:20)
      at _jestSnapshot (../node_modules/@jest/expect/build/index.js:15:16)
      at createJestExpect (../node_modules/@jest/expect/build/index.js:30:28)
      at Object.<anonymous> (../node_modules/@jest/expect/build/index.js:39:20)

这似乎是 semver 或 lru-cache 包的版本控制或配置问题,但我无法解决它。我已运行

yarn add semver --dev
yarn add lru-cache --dev
来查看是否可以解决该问题,但问题仍然存在。

这是根级别的package.json:

{
  // name, version, repository, author, and license omitted
  "private": true,
  "workspaces": [
    "webapp",
  ],
  "packageManager": "[email protected]",
  "dependencies": {
    "@jest/expect": "^29.7.0",
    "semver": "^7.6.0"
  }
}

然后在webapp/package.json中

{
  ...,
  "scripts": {
    ...
    "test": "jest --runInBand",
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...,
    "lru-cache": "^10.2.0",
   },
   "packageManager": "[email protected]"
}

根级别没有 tsconfig 或 jest 配置。在 webapp 目录中,配置如下所示: tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "paths": {
            "@/*": ["./src/*"]
        }
    },
    "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx",],
    "exclude": ["node_modules", "dist"],
    "ts-auto-mock": {
        "createMock": {
            "properties": "fake"
        }
    }
}

jest.config.js

const nextJest = require("next/jest");
const createJestConfig = nextJest({ dir: "./" });
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = createJestConfig({
    testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
    modulePathIgnorePatterns: ["<rootDir>/.*/__mocks__"],
    preset: "ts-jest",
    testEnvironment: "node",
    moduleDirectories: [
        "<rootDir>/node_modules",
        "<rootDir>/../node_modules",
        "<rootDir>/src",
    ],
    moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
    },
    transform: {
        "^.+\\.[tj]sx?$": "@swc/jest",
    },
    workerIdleMemoryLimit: "1024MB",
    testMatch: ["**/tests/**/test_*.(ts|tsx|js)"],
    moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
    setupFiles: ["<rootDir>/tests/setup.ts"],
    setupFilesAfterEnv: ["<rootDir>/tests/setupAfterEnv.ts"],
    cacheDirectory: ".jest/cache",
    cache: true,
    verbose: true,
});

以前,根级node_modules无法被识别,但现在可以了。但是,我无法解决有关 LRU 不是构造函数的问题。

javascript jestjs yarn-workspaces yarnpkg-v2
1个回答
0
投票

就我而言,我不确定 lru-cache 的导出结构是什么,所以我尝试记录输出

console.log(LRU);

并且看到了

{ LRUCache: [class LRUCache] }

所以我的解决方法是简单地将导入语句更新为

const LRU = require('lru-cache').LRUCache;

希望它能帮助别人!

© www.soinside.com 2019 - 2024. All rights reserved.