关于 angular-jest
面向 Angular 项目的 Jest 测试技能,使用 Jest 和 jest-preset-angular,涵盖测试配置与组件单元测试模式。
主要功能
- 01安装 Jest 测试依赖
- 02配置 jest-preset-angular
- 03编写组件单元测试
适用场景
- 01在 Angular 项目中配置 Jest
- 02使用 TestBed 测试 Angular 组件
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ oguzhan18/ angular-ecosystem-skills --skill angular-jest安装后在 Claude Code 中输入/angular-jest确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/discussions/_INDEX.mdreferences/docs/_INDEX.mdreferences/issues/_INDEX.mdreferences/releases/_INDEX.md详细介绍
Angular + Jest
版本: Jest 29.x(2025) 标签: Jest、测试、单元测试
参考: Jest • jest-preset-angular
最佳实践
- 安装 Jest
npm install --save-dev jest @types/jest jest-preset-angular
- 配置 Jest
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
};
- 编写测试
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();
});
});
评论
支持 Markdown