Angular + Jest: 无法解析AppComponent的所有参数:(?)
创始人
2024-10-14 12:31:10
0

这个错误通常是由于未正确配置测试环境导致的。以下是解决方法的示例代码:

  1. 确保你已经安装了Jest和相关的Angular测试库。可以通过运行以下命令来安装它们:
npm install jest @types/jest jest-preset-angular --save-dev
  1. 在项目的根目录下创建一个jest.config.js文件,并添加以下配置:
module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['/src/setup-jest.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$',
      astTransformers: {
        before: ['jest-preset-angular/build/InlineFilesTransformer', 'jest-preset-angular/build/StripStylesTransformer']
      }
    }
  }
};
  1. 在项目的src目录下创建一个setup-jest.ts文件,并添加以下内容:
import 'jest-preset-angular/setup-jest';
  1. 在项目的根目录下的tsconfig.spec.json文件中添加以下配置:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "jest",
      "node"
    ]
  },
  "files": [
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}
  1. 确保你的app.component.spec.ts文件中已正确导入AppComponent,并且使用了正确的测试装饰器。示例代码如下:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [AppComponent],
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create the app', () => {
    expect(component).toBeTruthy();
  });
});
  1. 运行npm test命令来执行测试。如果一切配置正确,你将不再看到"无法解析AppComponent的所有参数"的错误。

希望这些步骤能帮助你解决问题!

相关内容

热门资讯

透视挂透视!德普之星透视辅助软... 透视挂透视!德普之星透视辅助软件激活码(透视)都是是有挂(详细辅助揭秘教程);1、德普之星透视辅助软...
来一盘!hardrock透视工... 来一盘!hardrock透视工具,竞技联盟辅助,技巧教程(有挂技巧)1、玩家可以在hardrock透...
透视模拟器!wepoker亲友... 透视模拟器!wepoker亲友圈有用吗(透视)确实真的是有挂(详细辅助黑科技教程)一、软件透明挂的定...
分享实测!hardrock作弊... 分享实测!hardrock作弊,wpk透视辅助方法,可靠技巧(有挂透明)关于wpk透视辅助方法机制的...
透视app!wepoker透视... 透视app!wepoker透视脚本(透视)原来是真的有挂(详细辅助2025新版);亲,关键说明,we...
教学盘点!约局吧怎么看有没有挂... 1、教学盘点!约局吧怎么看有没有挂,wepoker能不能透视,必赢方法(有挂软件)(UU poker...
透视智能ai!来玩app 德州... 透视智能ai!来玩app 德州 辅助(透视)一贯真的有挂(详细辅助透明挂教程);1、机器人多个强度级...
一分钟揭秘!hhpoker到底... 1、一分钟揭秘!hhpoker到底可以作弊吗,impoker辅助,规律教程(有挂技巧)。2、hhpo...
透视规律!aapoker真的假... 透视规律!aapoker真的假的(透视)竟然真的是有挂(详细辅助我来教教你)暗藏猫腻,小编详细说明原...
玩家必看教程!xpoker透视... 玩家必看教程!xpoker透视辅助,wepoker软件安装包,曝光教程(有挂技巧);最新版2024是...