angular-jest
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAngular + Jest
Angular + Jest
Version: Jest 29.x (2025)
Tags: Jest, Testing, Unit Tests
References: Jest • jest-preset-angular
版本: Jest 29.x(2025年)
标签: Jest、测试、单元测试
参考资料: Jest • jest-preset-angular
Best Practices
最佳实践
- Install Jest
bash
npm install --save-dev jest @types/jest jest-preset-angular- Configure Jest
js
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
};- Write test
ts
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my.component';
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MyComponent]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
});- 安装Jest
bash
npm install --save-dev jest @types/jest jest-preset-angular- 配置Jest
js
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
};- 编写测试
ts
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my.component';
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MyComponent]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
});