简体   繁体   English

使用 angular 工作区配置 `jest-preset-angular`

[英]Configure `jest-preset-angular` with angular workspace

File structure:文件结构:

- projects
  - projectA
    - tsconfig.app.json
    - tsconfig.spec.json
  - projectB
    - tsconfig.app.json
    - tsconfig.spec.json
- jest.config.js
- setup-jest.ts
- tsconfig.base.json
- tsconfig.json

I followed the installation https://thymikee.github.io/jest-preset-angular/docs/getting-started/installation .我按照安装https://thymikee.github.io/jest-preset-angular/docs/getting-started/installation安装。

If I go with https://thymikee.github.io/jest-preset-angular/docs/guides/angular-ivy#control-ngcc-processing , it obviously doesn't find tsconfig.spec.json :如果我 go 与https://thymikee.github.io/jest-preset-angular/docs/guides/angular-ivy#control-ngcc-processing ,它显然找不到tsconfig.spec.json

File not found: <rootDir>/tsconfig.spec.json

In my case, I have one tsconfig.spec.json per project...就我而言,每个项目有一个tsconfig.spec.json ...

// jest.config.js
globalThis.ngJest = {
  skipNgcc: true,
  tsconfig: 'tsconfig.spec.json', // this is the project root tsconfig
};

module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  globalSetup: 'jest-preset-angular/global-setup',
  // https://thymikee.github.io/jest-preset-angular/docs/getting-started/installation#avoid-karma-conflicts
  testPathIgnorePatterns: [
    '/node_modules/',
    '<rootDir>/test.ts',
    '<rootDir>/projects/projectA/src/test.ts',
    '<rootDir>/projects/projectB/src/test.ts',
  ]
  // projects: [
  //   "<rootDir>/projects/fvl"
  // ]
};

Without the ngcc skip:没有 ngcc 跳过:

// jest.config.js
module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  globalSetup: 'jest-preset-angular/global-setup',
  // https://thymikee.github.io/jest-preset-angular/docs/getting-started/installation#avoid-karma-conflicts
  testPathIgnorePatterns: [
    '/node_modules/',
    '<rootDir>/test.ts',
    '<rootDir>/projects/projectA/src/test.ts',
    '<rootDir>/projects/projectB/src/test.ts',
  ]
  // projects: [
  //   "<rootDir>/projects/fvl"
  // ]
};

I get the ngcc error:我收到 ngcc 错误:

ngcc-jest-processor: running ngcc
Warning: The inferred tsconfig file "root_project/tsconfig.json" appears to be "solution-style" since it contains no root files but does contain project references.
This is probably not wanted, since ngcc is unable to infer settings like "paths" mappings from such a file.
Perhaps you should have explicitly specified one of the referenced projects using the --tsconfig option. For example:

  ngcc ... --tsconfig "./projects/projectA/tsconfig.app.json"
  ngcc ... --tsconfig "./projects/projectA/tsconfig.spec.json"
  ngcc ... --tsconfig "./projects/projectB/tsconfig.app.json"
  ngcc ... --tsconfig "./projects/projectB/tsconfig.spec.json"

I appreciate if someone has a working configuration with jest-preset-angular and angular workspaces.如果有人具有jest-preset-angular和 angular 工作区的工作配置,我将不胜感激。

I got it working with:我得到它的工作:

workspace-project/jest-config.js工作区项目/jest-config.js

module.exports = {
  preset: 'jest-preset-angular',
  globalSetup: 'jest-preset-angular/global-setup',
  setupFilesAfterEnv: [ '<rootDir>/../../setup-jest.ts' ]
};

workspace-project/setup-jest.ts工作区项目/setup-jest.ts

import 'jest-preset-angular/setup-jest';

workspace-project/projects/my-library1/jest.config.js工作区项目/项目/my-library1/jest.config.js

const baseConfig = require('../../jest.config');

module.exports = {
  ...baseConfig,
  coverageDirectory: '<rootDir>/../../dist/my-library1/__coverage'
};

workspace-project/tsconfig.spec.js工作区项目/tsconfig.spec.js

workspace-project/projects/my-library1/tsconfig.spec.json工作区项目/项目/my-library1/tsconfig.spec.json

...
"compilerOptions": {
  ...
  "module": "CommonJs",
  "types": [ "jest" ]
}

workspace-project/package.json工作区项目/package.json

...
"scripts": {
  ...
  "test:my-library1": "jest --config ./projects/my-library1/jest.config.js"
},

I hope it's usefull for someone!我希望它对某人有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相当于 jest-preset-angular 中的 and.callThrough() - Equivalent of and.callThrough() in jest-preset-angular 运行jest-preset-angleular时ENONET错误 - ENONET error while running jest-preset-angular Angular 8 和 jest - 找不到文件:jest-preset-angular/InlineHtmlStripStylesTransformer.js - Angular 8 and jest - File not found: jest-preset-angular/InlineHtmlStripStylesTransformer.js 如何在 jest.config.js 中设置 tsconfig 路径。 Angular 8. 开玩笑预设角度 - How to setup tsconfig paths in jest.config.js. Angular 8. jest-preset-angular Angular 4 + Jest-Preset-Angular:使用从文件导入的触发器在主机上测试动画 - Angular 4 + Jest-Preset-Angular : testing animation on host with trigger imported from file 模块<rootDir>未找到转换选项中的 /node_modules/jest-preset-angular/preprocessor.js - Module <rootDir>/node_modules/jest-preset-angular/preprocessor.js in the transform option was not found 为 Angular 配置 Jest - Configure Jest for Angular 将 Jest 配置为 Angular 5 - Configure Jest to Angular 5 如何使用 Jest 在 Angular 上配置 Allure - How to configure Allure on Angular with Jest 如果 Angular 项目在 NX 工作区中生成且预设为“空”,如何运行 e2e 测试? - How to run e2e tests if Angular project is generated in NX workspace with preset "empty"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM