2017-07-26 39 views
0

我正在使用Karma和Jasmine爲Angular 4應用程序編寫測試。我一直得到看起來像這樣的錯誤:爲Karma/Jasmine測試套件提供404個錯誤提取

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的應用程序,我可以在必要時提供配置了點。

是否有一個polyfill或我可以包括哪些可以讓Karma使用XMLHTTPRequest

+0

jasmine-query也會導致此問題。 –

+0

即使只是將'import {Observable}從'rxjs/Observable''更改爲'從'rxjs''導入{Observable}也會導致同樣的錯誤。 –

回答

-1

我有同樣的問題,我發現,合併另一個分支後,我正在努力,一些文件已經改變位置和業力尋找他們在舊的路徑。我只需重新啓動業務任務(在我的情況下也重新啓動IDE),問題就解決了。希望能幫助到你! ;-P