在AngularJS中,可以使用Jest来处理模块依赖关系。下面是一个示例解决方法,包含了代码示例:
npm install --save-dev jest jest-preset-angular @types/jest
jest.config.js
文件,并添加以下内容:module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['/src/setupJest.ts'],
moduleNameMapper: {
'@/(.*)': '/src/$1'
}
};
setupJest.ts
文件(在src
目录下),并添加以下内容:import 'jest-preset-angular';
import './polyfills';
// 在此处进行全局的测试配置,例如:设置适配器、模拟数据等
package.json
文件中添加以下脚本:"scripts": {
"test": "jest"
}
app.component.spec.ts
,并添加以下内容:import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
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).toBeTruthy();
});
});
npm test
这是一个基本的示例,您可以根据自己的项目结构和需求进行相应的修改和扩展。希望对您有帮助!