Angular KARMA与Jasmine + JEST或JASMINE + JEST,同一项目。
创始人
2024-10-19 10:01:12
0

解决方法1:使用Angular Karma和Jasmine

首先,确保已经安装了Angular CLI和Node.js。

  1. 在命令行中,进入项目的根目录,并运行以下命令安装Karma和Jasmine:
npm install karma jasmine karma-jasmine karma-chrome-launcher --save-dev
  1. 在项目根目录下创建一个karma.conf.js文件,并添加以下内容:
module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'src/**/*.spec.ts'
    ],
    exclude: [],
    preprocessors: {
      'src/**/*.spec.ts': ['@angular/cli']
    },
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('@angular/cli/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
  1. package.json文件中,将test脚本修改为:
"test": "ng test --code-coverage"
  1. 创建一个示例的测试文件src/app/app.component.spec.ts,并添加以下内容:
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';

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

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app');
  }));

  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
  }));
});
  1. 运行以下命令来执行测试:
npm test

解决方法2:使用Angular Karma和Jest

首先,确保已经安装了Angular CLI和Node.js。

  1. 在命令行中,进入项目的根目录,并运行以下命令安装Karma和Jest:
npm install karma jest @types/jest @angular-builders/jest --save-dev
  1. 在项目根目录下创建一个karma.conf.js文件,并添加以下内容:
module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jest'],
    files: [
      'src/**/*.spec.ts'
    ],
    exclude: [],
    preprocessors: {
      'src/**/*.spec.ts': ['@angular/cli']
    },
    plugins: [
      require('karma-jest'),
      require('@angular/cli/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
  1. package.json文件中,将test脚本修改为:
"test": "ng test --karma-config karma.conf.js"
  1. 创建一个示例的测试文件src/app/app.component.spec.ts,并添加以下内容:
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({

相关内容

热门资讯

三分钟窍门!wepoker好友... 三分钟窍门!wepoker好友房开挂(透视)本来是有辅助辅助(哔哩哔哩)1、点击下载安装,wepok...
第五分钟教程书!We poke... 第五分钟教程书!We poker辅助器下载(透视)本来有辅助神器(哔哩哔哩)一、We poker辅助...
第七分钟办法!wepoker轻... 第七分钟办法!wepoker轻量版透视方法(透视)竟然存在有辅助安装(哔哩哔哩)1、许多玩家不知道w...
五分钟手段!德扑圈透视(透视)... 五分钟手段!德扑圈透视(透视)真是真的有辅助插件(哔哩哔哩)暗藏猫腻,小编详细说明德扑圈透视破解器有...
第9分钟窍要!wepoker透... 第9分钟窍要!wepoker透视方法(透视)确实有辅助开挂(哔哩哔哩)所有人都在同一条线上,像星星一...
五分钟法子!hhpoker是真... 五分钟法子!hhpoker是真的还是假的(透视)总是有辅助辅助(哔哩哔哩)1、超多福利:超高返利,海...
第5分钟手筋!wpk俱乐部是做... 第5分钟手筋!wpk俱乐部是做什么的(透视)都是有辅助工具(哔哩哔哩)一、wpk俱乐部是做什么的可以...
第六分钟妙招!pokemmo脚... 第六分钟妙招!pokemmo脚本手机版(透视)真是是有辅助开挂(哔哩哔哩)1、每一步都需要思考,不同...
8分钟练习!wepoker怎么... 8分钟练习!wepoker怎么提高运气(透视)一直真的是有辅助下载(哔哩哔哩)1、首先打开wepok...
第4分钟手册!wpk作必弊(透... 第4分钟手册!wpk作必弊(透视)原来真的有辅助神器(哔哩哔哩)第4分钟手册!wpk作必弊(透视)原...