0

我正在使用 Karma 和 Jasmine 为 Angular 4 应用程序编写测试。我一直收到如下所示的错误:

Error: Fetch error: 404 Not Found
    at http://localhost:9876/base/node_modules/systemjs/dist/system.src.js?bf790cd9754a0743ba99db88ebac7a7c840dafaa:1500:13
    at ZoneDelegate.invoke (http://localhost:9876/base/node_modules/zone.js/dist/zone.js?082dc1d83c7a42ddda9b652319d281eef1d450d5:391:26)
    at Zone.run (http://localhost:9876/base/node_modules/zone.js/dist/zone.js?082dc1d83c7a42ddda9b652319d281eef1d450d5:141:43)
    at http://localhost:9876/base/node_modules/zone.js/dist/zone.js?082dc1d83c7a42ddda9b652319d281eef1d450d5:818:57
    at ZoneDelegate.invokeTask (http://localhost:9876/base/node_modules/zone.js/dist/zone.js?082dc1d83c7a42ddda9b652319d281eef1d450d5:424:31)
    at Zone.runTask (http://localhost:9876/base/node_modules/zone.js/dist/zone.js?082dc1d83c7a42ddda9b652319d281eef1d450d5:191:47)
    at drainMicroTaskQueue (http://localhost:9876/base/node_modules/zone.js/dist/zone.js?082dc1d83c7a42ddda9b652319d281eef1d450d5:584:35)
    at <anonymous>'

我查看了我尝试包含的库/模块,它们似乎都在使用XMLHTTPRequest()(或它的抽象)。这些包括mockBackendkarma-read-json

我的karma.conf.js文件如下:

// Karma configuration
//  Used for Angular app unit testing

'use strict';

var argv = require('yargs').argv;
var minimatch = require("minimatch");


module.exports = function (config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: './',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      // Polyfills.
      'node_modules/core-js/client/shim.min.js',
      'node_modules/intl/dist/Intl.min.js',

      'node_modules/traceur/bin/traceur.js',

      // System.js for module loading
      'node_modules/systemjs/dist/system.src.js',

      // Zone.js dependencies
      'node_modules/zone.js/dist/zone.js',
      'node_modules/zone.js/dist/long-stack-trace-zone.js',
      'node_modules/zone.js/dist/async-test.js',
      'node_modules/zone.js/dist/fake-async-test.js',
      'node_modules/zone.js/dist/sync-test.js',
      'node_modules/zone.js/dist/proxy.js',
      'node_modules/zone.js/dist/jasmine-patch.js',

      // karma-read-json
      { pattern: 'node_modules/karma-read-json/karma-read-json.js', included: false },

      // RxJs.
      { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
      { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },

      // paths loaded via module imports
      // Angular itself
      { pattern: 'node_modules/@angular/**/*.js', included: false, watched: true },
      { pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false },

      { pattern: 'dist/client/dev/**/*.js', included: false, watched: true },
      { pattern: 'dist/client/dev/**/*.html', included: false, watched: true, served: true },
      { pattern: 'dist/client/dev/**/*.css', included: false, watched: true, served: true },
      { pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false }, // PhantomJS2 (and possibly others) might require it

      // Dependencies for translation
      { pattern: 'node_modules/@ngx-translate/core/bundles/*.js', included:false, watched: false },
      { pattern: 'node_modules/@ngx-translate/http-loader/bundles/*.js', included:false, watched: false },

      // Other libraries
      { pattern: 'node_modules/lodash/*.js', included:false, watched: false },
      { pattern: 'node_modules/cron-converter/*.js', included:false, watched: false },
      { pattern: 'node_modules/cron-converter/src/*.js', included:false, watched: false },
      { pattern: 'node_modules/sprintf-js/src/*.js', included:false, watched: false },
      { pattern: 'node_modules/moment-timezone/*.js', included:false, watched: false },
      { pattern: 'node_modules/moment/*.js', included:false, watched: false },

      // suppress annoying 404 warnings for resources, images, etc.
      { pattern: 'dist/dev/assets/**/*', watched: false, included: false, served: true },

      // i18n files to be included for translate to work
      { pattern: 'src/client/assets/i18n/*.json', included: false},

      'test-config.js',
      'dist/client/dev/app/system-config.js',
      'test-main.js'
    ],

    // must go along with above, suppress annoying 404 warnings.
    proxies: {
      '/assets/': '/base/dist/dev/assets/'
    },

    // list of files to exclude
    exclude: [
      'node_modules/**/*spec.js'
    ],

    systemjs: {
      serveFiles: [
        'node_modules/**/*.js',
        'src/client/app/**/*.js',
        'src/client/assets/i18n/*.json'
      ]
    },

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['mocha'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: [
      'Chrome'
    ],


    customLaunchers: {
      Chrome_travis_ci: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Passing command line arguments to tests
    client: {
      files:  argv.files ? minimatch.makeRe(argv.files).source : null
    }
  });

  if (process.env.APPVEYOR) {
    config.browsers = ['IE'];
    config.singleRun = true;
    config.browserNoActivityTimeout = 90000; // Note: default value (10000) is not enough
  }

  if (process.env.TRAVIS || process.env.CIRCLECI) {
    config.browsers = ['Chrome_travis_ci'];
    config.singleRun = true;
    config.browserNoActivityTimeout = 90000;
  }
};

我正在开发的应用程序也使用 SystemJS 进行捆绑,如有必要,我可以为此提供配置。

是否有可以让 Karma 使用的 polyfill 或我可以包含的东西XMLHTTPRequest

4

1 回答 1

-1

我遇到了同样的问题,我发现,在将另一个分支合并到我正在处理的分支后,一些文件已经更改了位置,并且 karma 正在旧路径中寻找它们。我只需要重新启动业力任务(在我的情况下还需要重新启动 IDE),问题就解决了。希望能帮助到你!;-P

于 2017-08-02T11:11:10.887 回答