gpt4 book ai didi

angular - Monorepo 中的 Jest 项目无法在项目中找到配置文件

转载 作者:行者123 更新时间:2023-12-04 00:58:48 25 4
gpt4 key购买 nike

在设置 Jest 项目时遇到问题。
项目 A 有一些它运行的额外配置(setupJest,创建一些自定义匹配器等),而项目 B 没有。当我运行 jest 时,它尝试运行的每个测试都会出现此错误:

Test suite failed to run
File not found: <rootDir>/src/tsconfig.spec.json (resolved as: repo\src\tsconfig.spec.json)

repo 根目录中没有 src 文件夹,只有在各个项目文件夹中。我也没有在我的设置中的任何地方引用 src\tsconfig.spec.json。所以在这一点上我有点困惑要尝试什么。我试过把 rootDir 放在所有东西上并拼出路径,但似乎没有任何效果。任何建议、想法或帮助都会非常受欢迎。

作为引用,repo 是 Windows 驱动器上保存 repo 的文件夹。例如c:\公司名称\产品名称\

这是我的结构
repo
|- jest.config.js
|- projects
|- projectA
|- jest.config.js
|- tsconfig.spec.json
|- src
|- setupJest.js
|- projectB
|- jest.config.js
|- tsconfig.spec.json

根 jest.config.js:
module.exports = {
preset: 'jest-preset-angular',
projects: [
'<rootDir>/projects/projectA/jest.config.js',
'<rootDir>/projects/projectB/jest.config.js'
]
reporters: ['jest-silent-reporter'],
};

projectA jest.config.js:
module.exports = {
globals: {
'ts-jest': {
tsConfig: 'tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
diagnostics: false,
},
},
setupFilesAfterEnv: ['src/setupJest.ts'],
};

projectB jest.config.js:
module.exports = {
globals: {
'ts-jest': {
tsConfig: 'tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
diagnostics: false,
},
},
};

最佳答案

好的,经过 4 天的深入研究 github 上报告的问题和我能找到的一些 youtube 教程后,我搞砸了让它工作的方法。

根 jest.config.js

globals: {
'ts-jest': {
astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
diagnostics: false,
stringifyContentPathRegex: '\\.html$',
},
},
moduleDirectories: ['node_modules', './'],
modulePaths: ['node_modules', './'],
preset: 'ts-jest',
projects: ['projects/projectA', 'projects/projectB'],
reporters: ['jest-silent-reporter'],
testEnvironment: 'node',
transform: {
'\\.ts$': ['ts-jest'],
'\\.html$': ['ts-jest'],
},
verbose: true,

项目A/B jest.config.js
module.exports = {
globals: {
'ts-jest': {
astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
diagnostics: false,
stringifyContentPathRegex: '\\.html$',
tsConfig: '<rootDir>/projects/projectA/tsconfig.spec.json',
},
},
displayName: 'projectA',
moduleDirectories: ['node_modules', './'],
modulePaths: ['node_modules', './'],
name: 'projectA',
rootDir: './../../',
testMatch: ['<rootDir>/projects/projectA/**/*.spec.ts'],
transform: {
'\\.ts$': ['ts-jest'],
'\\.html$': ['ts-jest'],
},
};

事实证明,真正重要的一点是“rootDir: './../../'”部分。其余部分是特定于修复我处理它们时出现的其他错误,但是一旦我从子项目中正确定义了 rootDir,它实际上开始正确地看到所有这些错误。所以这是关键。 jest.config.js(不管它在项目中的哪个位置)定义了相对于自身的 rootDir。因此,当它到达项目文件夹并访问配置时,它重新映射了 rootDir 并丢失了。将子项目中的 root 定义为与父 jest 配置相同可以解决问题。在这一点上,Jest 的文档并不完全清楚。

关于angular - Monorepo 中的 Jest 项目无法在项目中找到配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60347361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com