天天看點

Angular應用隻執行指定單元測試的小技巧

以ng test storefrontlib為例,如果發現自己開發的單元測試出了問題需要調試,可以讓 Angular 隻運作自己出問題的那個單元測試:

Angular應用隻執行指定單元測試的小技巧
修改storefrontlib項目檔案夾src下面的test.ts, 改成如下内容:

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

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

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.

const FILE = ['./cms-components/checkout/components/delivery-mode/delivery-mode.component.spec.ts'];

context.keys().filter( name => !!FILE.includes(name)).map(context);
      

這樣就達到了我們想要的效果:

Angular應用隻執行指定單元測試的小技巧

繼續閱讀