Angular CLI是一个用于快速搭建Angular应用的命令行工具。默认情况下,Angular CLI使用Jasmine作为测试框架。如果想将新创建的Angular CLI项目转换为使用Mocha框架而不是Jasmine,可以按照以下步骤进行操作:
npm install -g @angular/cli
ng new my-app
cd my-app
npm install --save-dev mocha chai @types/mocha @types/chai
ng generate component app
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { expect } from 'chai';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).to.be.ok;
});
it(`should have as title 'my-app'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).to.equal('my-app');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('.content span').textContent).to.contain('my-app app is running!');
});
});
ng test
这样,新创建的Angular CLI项目就成功转换为使用Mocha框架进行测试了。
关于Mocha和Chai的更多信息,可以参考以下链接:
请注意,以上答案仅供参考,具体操作可能因个人环境和需求而有所差异。
领取专属 10元无门槛券
手把手带您无忧上云