使用业力茉莉与子进程进行单元测试

时间:2019-02-24 15:04:29

标签: typescript unit-testing karma-jasmine

问候,

我目前正在尝试在使用Angular和Electron的应用程序上使用业力茉莉花创建测试。

我遇到了使用子进程的simple-git问题,似乎引起了该问题:

“ ./ node_modules / simple-git / src / index.js中的错误 找不到模块:错误:无法解析“ C:\ Users \ Martin \ Documents \ Cours \ Projet \ Githarpon \ git-harpon \ node_modules \ simple-git \ src”中的“ child_process” 在“ C:\ Users \ Martin \ Documents \ Cours \ Projet \ Githarpon \ git-harpon \ node_modules \ simple-git \ src”中解析“ child_process””

然后,我尝试使用simple-git(GitService)模拟我的服务

    TestBed.configureTestingModule({
      declarations: [HomeComponent],
      imports: [
        FormsModule
      ],
      providers: [
        {
          provide: TranslateService,
          useClass: MockTranslateService
        },
        {
            provide: GitService,
            useClass: MockGitService
        },
        {
            provide: ElectronService,
            useClass: MockElectronService
        }
      ]
    })
      .compileComponents();
  }));

MockService:

import * as gitPromise from 'simple-git/promise';
import * as simpleGit from 'simple-git';
import { Subject } from 'rxjs';
import { ElectronService } from '../providers/electron.service';
import * as  GitUrlParse from 'git-url-parse';
import { ServiceResult } from '../models/ServiceResult';
import { TranslateService } from '@ngx-translate/core';

export class MockGitService {
}

最后是我的karma.conf.js文件:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-htmlfile-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    htmlReporter: {
      outputFile: '../units/units.html'
    },
    reporters: ['progress', 'html'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: true
  });
};

非常欢迎任何帮助

0 个答案:

没有答案