myc*_*t16 5 typescript jestjs monorepo angular
Having trouble setting up jest projects. Project A has some additional configuration that it runs (setupJest, creates some custom matchers, etc) that project B does not have. When I run jest I get this error for every single test it tries to run:
Test suite failed to run
File not found: <rootDir>/src/tsconfig.spec.json (resolved as: repo\src\tsconfig.spec.json)
Run Code Online (Sandbox Code Playgroud)
There is no src folder in the repo root, only inside the individual project folders. I'm also not referencing src\tsconfig.spec.json anywhere in my setups. So I'm a bit confused at this point as to what to try. I've tried putting rootDir on everything and spelling out the paths, but nothing seems to work. Any suggestions or ideas or help would be REALLY welcome.
For reference, repo is a folder on a windows drive that holds the repo. e.g. c:\companyName\productName\
This is my structure
repo
|- jest.config.js
|- projects
|- projectA
|- jest.config.js
|- tsconfig.spec.json
|- src
|- setupJest.js
|- projectB
|- jest.config.js
|- tsconfig.spec.json
Run Code Online (Sandbox Code Playgroud)
root 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'],
};
Run Code Online (Sandbox Code Playgroud)
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'],
};
Run Code Online (Sandbox Code Playgroud)
projectB jest.config.js:
module.exports = {
globals: {
'ts-jest': {
tsConfig: 'tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
diagnostics: false,
},
},
};
Run Code Online (Sandbox Code Playgroud)
好的,经过 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,
Run Code Online (Sandbox Code Playgroud)
项目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'],
},
};
Run Code Online (Sandbox Code Playgroud)
事实证明,真正重要的一点是“rootDir: './../../'”部分。其余部分是特定于修复在我处理它们时出现的其他错误,但是一旦我从子项目中正确定义了 rootDir,它实际上开始正确地看到所有这些错误。所以这是关键。jest.config.js(不管它在项目中的哪个位置)定义了相对于自身的 rootDir。因此,当它到达项目文件夹并访问配置时,它重新映射了 rootDir 并丢失了。将子项目中的 root 定义为与父 jest 配置相同可以解决问题。在这一点上,Jest 的文档并不完全清楚。
| 归档时间: |
|
| 查看次数: |
2936 次 |
| 最近记录: |