在Angular应用中,可以使用Jest来处理导入入口点。以下是一个使用Jest测试Angular组件的示例解决方案:
npm install jest @types/jest ts-jest jest-preset-angular --save-dev
jest.config.js
文件,并添加以下内容:module.exports = {
preset: "jest-preset-angular",
setupFilesAfterEnv: [
"/src/setup-jest.ts"
],
collectCoverage: true,
coverageReporters: ["html"]
};
src
目录下创建一个setup-jest.ts
文件,并添加以下内容:import 'jest-preset-angular';
package.json
文件中,将测试命令test
修改为以下内容:"test": "jest"
component.spec.ts
文件中添加以下代码示例:import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'my-app'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('my-app');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('.content span').textContent).toContain('my-app app is running!');
});
});
npm test
命令来执行Jest测试。将会显示测试结果的报告。这样就可以使用Jest来处理Angular应用的导入入口点。