为什么 Jest 在 node_modules 中找不到模块,但我的应用可以?

Why can't Jest find a module in node_modules, but my app can?

提问人:standing jon 提问时间:11/17/2023 更新时间:11/17/2023 访问量:24

问:

我有一个使用 jsgrowup2 的 Vue3 应用程序,这是一个我(不熟练地)编写的库。在浏览器的应用程序中导入它工作正常,但是当我使用 Jest 运行单元测试时,它们失败并出现错误

Cannot find module 'jsgrowup2' from 'src/models/GrowthAssessment.ts'

它失败的 import 语句是:

import { Observation, SexSpecification } from "jsgrowup2"

我怀疑 jsgrowup2 的构建过程在某种程度上存在问题,但就其价值而言,这是我为 Vue 应用程序提供的 jest 配置和 tsconfig:

// jest.config.js
export default {
  preset: "ts-jest",
  transform: {
    "^.+\\.(ts|tsx)?$": "ts-jest",
    "^.+\\.(js|jsx)$": "babel-jest",
  },
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
}

// tsconfig.json
{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "isolatedModules": true,
    "noEmit": true,
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "exclude": ["./cypress.config.ts", "node_modules", "cypress", "**/*.cy.tsx"]
}

我尝试清除开玩笑的缓存,但无济于事。

./node_modules/.bin/jest --clearCache

我尝试编辑 's 以包含“main”属性(根据此评论)。jsgrowup2package.json

"main": "dist/index.mjs",

没有骰子。

打字稿 jestjs

评论


答: 暂无答案