首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >monorepo包中jest config中的目标monorepo根目录

monorepo包中jest config中的目标monorepo根目录
EN

Stack Overflow用户
提问于 2022-11-14 12:22:03
回答 2查看 76关注 0票数 3

我的monorepo中有以下文件结构

代码语言:javascript
复制
monorepo
 ┣ node_modules
 ┣ packages
 ┃ ┣ package-1
 ┃ ┃ ┗ jest.config.js
 ┃ ┣ package-2
 ┃ ┃ ┗ jest.config.js
 ┃ ┣ package-3
 ┃ ┃ ┗ jest.config.js
 ┣ package.json
 ┗ jest.base.config.js

在monorepo根目录中,我有一个文件jest.base.config.js,它包含

代码语言:javascript
复制
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80,
    },
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  transformIgnorePatterns: [
    'node_modules/(?!((jest-)?react-native|@react-native(-community)?|@fortawesome/react-native-fontawesome|d3-.*|internmap)/)',
  ],
};

然后,在每个包jest.config.js中,我导入了jest.base.config并通过任何重写导出它,如下所示

代码语言:javascript
复制
const baseConfig = require('../../jest.base.config');

module.exports = {
  ...baseConfig,
  testPathIgnorePatterns: ['<rootDir>/cypress/'],
  collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}', '!src/**/*.style.{ts,js}'],
  moduleNameMapper: {
    '^.+\\.svg$': '<rootDir>/__mocks__/mockSVG.js',
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|pdf)$':
      '<rootDir>/__mocks__/mockFile.js',
    '\\.(css|less)$': 'identity-obj-proxy',
  },
  globals: {
    __WEB__: true,
    google: {},
  },
  setupFiles: ['./jest.setup.js'],
  testURL: 'http://localhost:3000',
};

但是,需要映射程序的包@uiw/react-textarea-code-editor (在此发行)中有一个问题,因此尝试将其映射到公共js文件,方法是将它作为属性添加到jest.config.js文件中的moduleNameMapper属性中。

代码语言:javascript
复制
moduleNameMapper: {
  ...
  '@uiw/react-textarea-code-editor': '<rootDir>/node_modules/@uiw/react-textarea-code-editor/dist/editor.js',
},

这在我所拥有的常规存储库上有效,但这里的问题是<rootDir>/node_modules针对的是./packages/package-1/node_modules,而不是monorepo根目录上的./node_modules,因为@uiw目录就在那里。

现在,我知道我可以将rootDir更改为rootDir: './../../',,但这将抛出对rootDir的所有其他引用。

我试过了

代码语言:javascript
复制
moduleNameMapper: {
  ...
  '@uiw/react-textarea-code-editor': '../../node_modules/@uiw/react-textarea-code-editor/dist/editor.js',
},

哪个应该以./node_modules/@uiw/react-textarea-code-editor/dist/editor.js为目标

但这给了我以下错误

代码语言:javascript
复制
Configuration error:
    
    Could not locate module @uiw/react-textarea-code-editor mapped as:
    ../../node_modules/@uiw/react-textarea-code-editor/dist/editor.js.
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/@uiw\/react-textarea-code-editor/": "../../node_modules/@uiw/react-textarea-code-editor/dist/editor.js"
      },
      "resolver": undefined
    }

有比更改rootDir值更优雅的解决方案吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-24 01:14:25

你能使用path.resolve吗?

代码语言:javascript
复制
moduleNameMapper: {
  '@uiw/react-textarea-code-editor': path.resolve(
     __dirname, 
    '../../node_modules/@uiw/react-textarea-code-editor/dist/editor.js'
  ),
},

<root>的相对路径

代码语言:javascript
复制
moduleNameMapper: {
  '@uiw/react-textarea-code-editor': '<rootDir>/../../node_modules/@uiw/react-textarea-code-editor/dist/editor.js'
},
票数 3
EN

Stack Overflow用户

发布于 2022-11-24 00:35:10

基于与问题有关,我相信这句话正确地识别了问题。一旦@uiw/react-textarea-code-editor的维护人员决定使用只支持ESM的rehype版本,他们就应该在下一个主要版本中取消对CJS的支持。

如果您可以降级您的@uiw/react-textarea-code-editor版本,您可以使用npm i @uiw/react-textarea-code-editor@1.3.1,这是依赖于CommonJS版本的rehype的最新版本。

或者,如果您可以将当前版本的@uiw/react-textarea-code-editor与不同版本的rehype混合,并使用支持传递依赖解决(例如resolutions for yarnoverrides for npm )的包管理器,则可以使用CommonJS将最新版本降级为rehype@11.0.0

例如,使用npm

代码语言:javascript
复制
  "overrides": {
    "rehype": "11.0.0"
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74431579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档