Jest.js 无法找到模块“msw/node "

rqdpfwrv  于 2023-11-15  发布在  Jest
关注(0)|答案(1)|浏览(163)

我正在使用MSW模拟服务器请求。我最近更新到v2,之后在尝试从msw/node导入路径导入setupServer时开始看到module not found错误。我可以在我的节点模块文件夹中看到该文件,我知道它在那里。是什么导致了这个问题?我如何修复它?
节点版本:20.9.0(当前LTS)
Pnpm版本:8.10.0
package.json

{
  "dependencies": {
    "@emotion/cache": "^11.11.0",
    "@emotion/react": "^11.11.1",
    "@emotion/server": "^11.11.0",
    "@emotion/styled": "^11.11.0",
    "@hookform/resolvers": "^3.3.2",
    "@mui/material": "^5.14.15",
    "@mui/icons-material": "^5.14.15",
    "awilix": "^9.0.0",
    "axios": "^1.6.0",
    "cookies": "^0.8.0",
    "joi": "^17.11.0",
    "joi-password": "^4.1.1",
    "jotai": "^2.5.0",
    "js-cookie": "^3.0.5",
    "lodash": "^4.17.21",
    "next": "^14.0.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-hook-form": "^7.47.0",
    "zxcvbn": "^4.4.2"
  },
  "devDependencies": {
    "@babel/core": "^7.23.2",
    "@playwright/test": "^1.39.0",
    "@testing-library/dom": "^9.3.3",
    "@testing-library/jest-dom": "^6.1.4",
    "@testing-library/react": "^14.0.0",
    "@testing-library/user-event": "^14.5.1",
    "@types/cookies": "^0.7.9",
    "@types/jest": "^29.5.7",
    "@types/js-cookie": "^3.0.5",
    "@types/jsdom": "^21.1.4",
    "@types/lodash": "^4.14.200",
    "@types/node": "^20.8.10",
    "@types/react": "^18.2.33",
    "@types/zxcvbn": "^4.4.3",
    "@typescript-eslint/eslint-plugin": "^6.9.1",
    "@typescript-eslint/parser": "^6.9.1",
    "eslint": "^8.52.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-airbnb-typescript": "^17.1.0",
    "eslint-config-next": "^14.0.1",
    "eslint-config-standard": "^17.1.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-jest": "^27.6.0",
    "ignore-loader": "^0.1.2",
    "jest": "^29.7.0",
    "jest-environment-jsdom": "^29.7.0",
    "msw": "^2.0.1",
    "node-fetch": "^3.3.2",
    "react-test-renderer": "^18.2.0",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2",
    "whatwg-fetch": "^3.6.19"
  }
}

字符串
ts配置

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "types": ["node", "jest", "@testing-library/jest-dom"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "./jest.setup.ts"],
  "exclude": ["node_modules"]
}


jest配置

import nextJest from 'next/jest';
import { Config } from 'jest';

module.exports = nextJest({ dir: '.' })({
  roots: ['<rootDir>/src'],
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  moduleDirectories: ['node_modules', '<rootDir>/'],
  testEnvironment: 'jsdom',
  collectCoverageFrom: ['./src/**/*.{ts,tsx}'],
} as Config);


jest安装文件

import '@testing-library/jest-dom';
import 'whatwg-fetch';


发生错误的文件

import { setupServer } from 'msw/node';
import { handlers } from './handlers';

export const server = setupServer(...handlers);

export default server;


误差

Test suite failed to run

    Cannot find module 'msw/node' from 'path/to/file.ts'

mqkwyuun

mqkwyuun1#

您的问题可能与从v1到v2的msw迁移有关。在文档中,您可以找到此处描述的问题
从docs:
此错误由测试运行器引发,因为JSDOM默认使用浏览器导出条件。这意味着当您导入任何第三方包(如MSW)时,JSDOM会强制将其浏览器导出用作入口点。这是不正确和危险的,因为JSDOM仍然在Node.js中运行,并且无法保证完全的浏览器兼容性。
要解决此问题,请将jest.config.js中的testEnvironmentOptions.customExportConditions选项设置为['']

// jest.config.js
module.exports = {
  testEnvironmentOptions: {
    customExportConditions: [''],
  },
}

字符串
这将强制JSDOM在导入msw/node时使用默认的导出条件,从而导致正确的导入。

相关问题