天天看點

解決 ng test jasminerequire not defined問題

項目做單元測試的時候運作ng test 報錯 jasminerequire not defined

上網查了很多資料最後總結出兩個辦法。

第一個方法是是在你的

karma.conf.js

代碼中,把

reporters: ['progress', 'kjhtml']

require('karma-jasmine-html-reporter')

注釋掉。

重新運作ng test 可以運作。缺點是html錯誤資訊頁面不展示,隻在控制台顯示。

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

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('@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', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    // reporters: ['progress', 'kjhtml'], // 注釋
    reporters: ['progress', 'coverage-istanbul'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
           

第二個方法是最建議的解決辦法。删除你的node_modules包,使用npm安裝,一定不要使用cnpm安裝。

如果被牆也可使用

npm install --registry=https://registry.npm.taobao.org