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的所有参数"的错误。

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

相关内容

热门资讯

透视解迷!wepoker免费透... 透视解迷!wepoker免费透视(透视)wpk透视是真的吗,教程方针(有挂技巧)-哔哩哔哩1、全新机...
透视总结!hh poker透视... 透视总结!hh poker透视器下载(透视)fishpoker透视,教程演示(有挂神器)-哔哩哔哩1...
透视必备!wepoker祈福有... 透视必备!wepoker祈福有用吗(透视)wpk模拟器,教程方针(有挂神器)-哔哩哔哩1)wepok...
透视有挂!wepoker如何设... 透视有挂!wepoker如何设置透视(透视)wepoker可以开透视吗,教程讲义(有挂技术)-哔哩哔...
透视总结!wepoker免费辅... 透视总结!wepoker免费辅助器(透视)德普之星辅助工具如何打开,教程要领(有挂猫腻)-哔哩哔哩小...
透视解密!wepoker如何设... 透视解密!wepoker如何设置透视(透视)佛手在线大菠萝为什么都输,教程指南书(有人有挂)-哔哩哔...
透视开挂!wepoker有透视... 透视开挂!wepoker有透视底牌吗(透视)pokemmo脚本手机版,教程法子(有挂存在)-哔哩哔哩...
透视解密!wepoker有插件... 透视解密!wepoker有插件吗(透视)hhpoker破解工具,教程法门(有挂教学)-哔哩哔哩;1、...
透视曝光!pokemmo辅助器... 透视曝光!pokemmo辅助器脚本下载(透视)wepoker私人局怎么玩,教程模板(真的有挂)-哔哩...
透视关于!wepoker底牌透... 透视关于!wepoker底牌透视(透视)wepoker手机版辅助,教程课程(的确有挂)-哔哩哔哩运w...