2017-03-19 21 views
1

https://github.com/angular/quickstartのangle2-seedを使用しています。自分のクラスの単体テストをしたい。以下は私のテストで:カルマ:XHRエラー(404が見つかりません)ロードsrc/node_modules/angular2-jwt/angular2-jwt.js

describe('my method',() => { 
    it('should expect true',() => { 
     mockContextSettings = jasmine.createSpyObj('mockContextSettings', ['environment']); 
     mockWindow = jasmine.createSpyObj('mockWindow', ['location']); 
     workContext = new WorkContext(mockContextSettings, mockWindow); 
     expect(true).toBe(true); 
    }); 
}); 

テストは

workContext = new WorkContext(mockContextSettings, mockWindow); 

なくても正常に動作します。しかし、私は私のテストでそれを持っているとき、私はカルマのブラウザコンソールで次のエラーが表示されます。

GET http://localhost:9876/base/src/node_modules/angular2-jwt/angular2-jwt.js 404 (Not Found) 

以下は私のsystemjs.config、karma.config.js、karma-test-shim.jsです:

systemjs.config

/** 
* System configuration for Angular samples 
* Adjust as necessary for your application needs. 
*/ 
(function (global) { 
    System.config({ 
    defaultJSExtensions: true, 
    paths: { 
     // paths serve as alias 
     'npm:': 'node_modules/' 
    }, 
    // map tells the System loader where to look for things 
    map: { 
     // our app is within the app folder 
     app: 'app', 

     // angular bundles 
     '@angular/core': 'npm:@angular/core/bundles/core.umd.js', 
     '@angular/common': 'npm:@angular/common/bundles/common.umd.js', 
     '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', 
     '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', 
     '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', 
     '@angular/http': 'npm:@angular/http/bundles/http.umd.js', 
     '@angular/router': 'npm:@angular/router/bundles/router.umd.js', 
     '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', 

     // other libraries 
     'rxjs':      'npm:rxjs', 
     'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js', 
     'angular2-jwt': 'npm:angular2-jwt/angular2-jwt.js', 
     'moment': 'npm:moment/min/moment.min.js', 
     'moment-timezone': 'npm:moment-timezone/builds/moment-timezone-with-data-2010-2020.min.js' 
    }, 
    // packages tells the System loader how to load when no filename and/or no extension 
    packages: { 
     app: { 
     defaultExtension: 'js' 
     }, 
     rxjs: { 
     defaultExtension: 'js' 
     } 
    } 
    }); 
})(this); 

karma.config.js

module.exports = function(config) { 

    var appBase = 'src/';  // transpiled app JS and map files 
    var appSrcBase = appBase;  // app source TS files 

    // Testing helpers (optional) are conventionally in a folder called `testing` 
    var testingBase = 'testing/'; // transpiled test JS and map files 
    var testingSrcBase = 'testing/'; // test source TS files 

    config.set({ 
    basePath: '.', 
    frameworks: ['jasmine'], 

    plugins: [ 
     require('karma-jasmine'), 
     require('karma-chrome-launcher'), 
     require('karma-jasmine-html-reporter') 
    ], 

    client: { 
     builtPaths: [appBase, testingBase], // add more spec base paths as needed 
     clearContext: false // leave Jasmine Spec Runner output visible in browser 
    }, 

    customLaunchers: { 
     // From the CLI. Not used here but interesting 
     // chrome setup for travis CI using chromium 
     Chrome_travis_ci: { 
     base: 'Chrome', 
     flags: ['--no-sandbox'] 
     } 
    }, 

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

     // Polyfills 
     'node_modules/core-js/client/shim.js', 

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

     'node_modules/angular2-jwt/angular2-jwt.js', // <-- added explicitly to see if it fixes the issue 

     // 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: false }, 
     { pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false }, 

     { pattern: appBase + '/systemjs.config.js', included: false, watched: false }, 
     { pattern: appBase + '/systemjs.config.extras.js', included: false, watched: false }, 
     'karma-test-shim.js', // optionally extend SystemJS mapping e.g., with barrels 

     // transpiled application & spec code paths loaded via module imports 
     { pattern: appBase + '**/*.js', included: false, watched: true }, 
     { pattern: testingBase + '**/*.js', included: false, watched: true }, 


     // Asset (HTML & CSS) paths loaded via Angular's component compiler 
     // (these paths need to be rewritten, see proxies section) 
     { pattern: appBase + '**/*.html', included: false, watched: true }, 
     { pattern: appBase + '**/*.css', included: false, watched: true }, 

     // Paths for debugging with source maps in dev tools 
     { pattern: appBase + '**/*.ts', included: false, watched: false }, 
     { pattern: appBase + '**/*.js.map', included: false, watched: false }, 
     { pattern: testingSrcBase + '**/*.ts', included: false, watched: false }, 
     { pattern: testingBase + '**/*.js.map', included: false, watched: false} 
    ], 

    // Proxied base paths for loading assets 
    proxies: { 
     // required for modules fetched by SystemJS 
     '/base/src/node_modules/': '/base/node_modules/' 
    }, 

    exclude: [], 
    preprocessors: {}, 
    reporters: ['progress', 'kjhtml'], 

    port: 9876, 
    colors: true, 
    logLevel: config.LOG_INFO, 
    autoWatch: true, 
    browsers: ['Chrome'], 
    singleRun: false 
    }) 
} 

カルマ・テストshim.js

// /*global jasmine, __karma__, window*/ 
Error.stackTraceLimit = 0; // "No stacktrace"" is usually best for app testing. 

// Uncomment to get full stacktrace output. Sometimes helpful, usually not. 
// Error.stackTraceLimit = Infinity; // 

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; 

// builtPaths: root paths for output ("built") files 
// get from karma.config.js, then prefix with '/base/' (default is 'src/') 
var builtPaths = (__karma__.config.builtPaths || ['src/']) 
       .map(function(p) { return '/base/'+p;}); 

__karma__.loaded = function() { }; 

function isJsFile(path) { 
    return path.slice(-3) == '.js'; 
} 

function isSpecFile(path) { 
    return /\.spec\.(.*\.)?js$/.test(path); 
} 

// Is a "built" file if is JavaScript file in one of the "built" folders 
function isBuiltFile(path) { 
    return isJsFile(path) && 
     builtPaths.reduce(function(keep, bp) { 
      return keep || (path.substr(0, bp.length) === bp); 
     }, false); 
} 

var allSpecFiles = Object.keys(window.__karma__.files) 
    .filter(isSpecFile) 
    .filter(isBuiltFile); 

System.config({ 
    // Base URL for System.js calls. 'base/' is where Karma serves files from. 
    baseURL: 'base/src', 
    // Extend usual application package list with test folder 
    packages: { 'testing': { main: 'index.js', defaultExtension: 'js' } }, 

    // Assume npm: is set in `paths` in systemjs.config 
    // Map the angular testing umd bundles 
    map: { 
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js', 
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js', 
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js', 
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js', 
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js', 
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js', 
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js', 
    '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js', 
    'angular2-jwt': 'npm:angular2-jwt/angular2-jwt.js', // <-- added explicitly to see if it fixes the issue 
    }, 
}); 

System.import('systemjs.config.js') 
    .then(importSystemJsExtras) 
    .then(initTestBed) 
    .then(initTesting); 

/** Optional SystemJS configuration extras. Keep going w/o it */ 
function importSystemJsExtras(){ 
    return System.import('systemjs.config.extras.js') 
    .catch(function(reason) { 
    console.log(
     'Warning: System.import could not load the optional "systemjs.config.extras.js". Did you omit it by accident? Continuing without it.' 
    ); 
    console.log(reason); 
    }); 
} 

function initTestBed(){ 
    return Promise.all([ 
    System.import('@angular/core/testing'), 
    System.import('@angular/platform-browser-dynamic/testing') 
    ]) 

    .then(function (providers) { 
    var coreTesting = providers[0]; 
    var browserTesting = providers[1]; 

    coreTesting.TestBed.initTestEnvironment(
     browserTesting.BrowserDynamicTestingModule, 
     browserTesting.platformBrowserDynamicTesting()); 
    }) 
} 

// Import all spec files and start karma 
function initTesting() { 
    return Promise.all(
    allSpecFiles.map(function (moduleName) { 
     return System.import(moduleName); 
    }) 
) 
    .then(__karma__.start, __karma__.error); 
} 

私は私のパッケージでangular2-JWTを持っています.jsonの依存関係を確認してインストールしてください。私は本当に問題が何であるか分からない。

UPDATE:

が再びテストを実行し、理由もなく、この時間は、それがangular2-jwt.jsをロードします!しかし、まだ別の問題:このファイルは行があります(「...」)が必要ですので、私はカルマブラウザのコンソールで次のエラーにこの時間を取得:

Uncaught ReferenceError: require is not defined 
    at node_modules/angular2-jwt/angular2-jwt.js:19 

答えて

1

{ pattern: 'node_modules/angular2-jwt/angular2-jwt.js', included: false, watched: false } 
01 ...に... ...変更

'node_modules/angular2-jwt/angular2-jwt.js' 

をお試しくださいあなたのkarma.config.jsファイルの

...ファイルを含む

{ pattern: "your/file/path", included: true, watched: false } 

を次のようにしている

コードの最初の行は、ファイルのデフォルト値を使用するようにカルマを伝えますが、何でないスクリプトタグを使用して、ファイルをロードするためにカルマを伝えますsystemjsを使用してアプリケーションをロードしたい場合

今日、私はこのトリッキーな設定について読んでいました。興味がある場合は、以下の記事へのリンクを貼り付けました。私はそれが非常に役立つことがわかった。

https://psamsotha.github.io/angular/2016/12/16/angular2-testing-karma-systemjs.html

+1

うわー!問題を修正しました。驚くばかり!私はこの間何日も挫折しました!どうもありがとう!心から感謝する! – hosjay

+1

あなたは確かに歓迎です。それがあなたのために働いてうれしい。 –

0

としてkarma.config.jsでのappBaseを変更
var appBase = '.';  ///////////////////////////////// 
+0

おかげ@Aravindが、私はその後、systemjsのロードに失敗することを行った場合。 – hosjay

+0

'appSrcBase = 'src /'' karmaconfigで試してください – Aravind

+0

私は自分の答えを更新しました。更新セクションをご覧ください。 – hosjay

関連する問題