角度测试 - 排除特定规范文件

Angular tests - Exclude specific spec files

提问人:Amphyx 提问时间:11/17/2023 更新时间:11/17/2023 访问量:29

问:

我在 Angular 项目的某些规范文件中有错误,并希望将它们从编译中排除。

我把这些文件放在:excludetsconfig.spec.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ],
  "exclude": [
    "src/**/ms-dialog.spec.ts"  // Should be excluded
  ]
}

但是当我执行时,出现以下错误:ng test

./src/app/core/models/ms-dialog.spec.ts - Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):
Error: /home/user/Documents/Projects/my-project/src/app/core/models/ms-dialog.spec.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

可能是我文件中的原因:tsconfig.base.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "useDefineForClassFields": true,
    "module": "es2020",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2022",
    "typeRoots": ["node_modules/@types"],
    "types": ["node"],
    "lib": ["es2018", "dom"],
    "paths": {
      "core-js/es7/reflect": [
        "../node_modules/core-js/proposals/reflect-metadata"
      ],
      "core-js/es6/*": ["../node_modules/core-js/es/*"],
      "@env": ["./environments/environment"],
      "@shared/*": ["./app/shared/*"],
      "@core/*": ["./app/core/*"],
    }
  }
}

还有我的:test.ts

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
  teardown: { destroyAfterEach: false },
});
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
角度 测试 业力-茉莉花

评论

0赞 Andrei 11/17/2023
也许可以帮助你?这在某种程度上标记了一个描述,以跳过其中的所有测试xdescribe
0赞 Amphyx 11/17/2023
@Andrei,很遗憾,没有,因为我的规范文件中有一个 TS 错误。在我的项目中有很多这样的人。我想排除它们,而不是现在花时间修复它们
0赞 Andrei 11/17/2023
您可以保持 tsconfig 不变,但修改test.ts以不需要您不需要的文件
0赞 Amphyx 11/17/2023
@Andrei,我已经尝试过了(是的,在 ),但是我从所有规范文件中都收到错误:test.ts<file> is missing from the TypeScript compilation
0赞 Andrei 11/18/2023
你试过吗?context.keys().filter(...filterlogic...).map(context)

答: 暂无答案