2016-09-29 251 views
0

我正在嘗試從quickstart編寫Angular 2應用程序的測試用例。如何更改spec文件的路徑

每當我運行代碼時,spec文件總是生成在同一個文件夾中,但我想移動到自定義測試文件夾。我正在使用SystemJS。例如,app.component.spec.js應移動到testing文件夾。

+0

你看過你的'karma.conf.js'嗎?你爲什麼不想使用這個約定? – jonrsharpe

+0

我看過這個 var testBase ='testing /'; //測試JS和映射文件 var testSrcBase ='testing /'; //測試源TS文件 如何在測試文件夾中移動這些specs.js文件? 它給了我警告 app/**/* .css「與任何文件都不匹配 testing/**/*。js.map」與任何文件都不匹配。 – fruitjs

+1

這個警告只是因爲你沒有.js.map文件和.css文件,這就是全部;) – Supamiu

回答

3

好的,所以我有我的測試文件在/test,以及karma.conf.jskarma-test-shim.js

我的目錄結構是這樣的:
我的項目
| --app
| --node_modules
| --test
.. | --app.component.spec.ts
.. | --karma.conf.js
.. | --karma試驗shim.js

這裏是我的karma.conf.js文件看起來像:

module.exports = function(config) { 

    var appBase = 'app/';  // transpiled app JS and map files 
    var appSrcBase = 'app/';  // app source TS files 
    var appAssets = '/base/app/'; // component assets fetched by Angular's compiler 

    var testBase = 'test/';  // transpiled test JS and map files 
    var testSrcBase = 'test/';  // test source TS files 

    config.set({ 
    basePath: '../', 
    frameworks: ['jasmine'], 
    plugins: [ 
     require('karma-jasmine'), 
     require('karma-chrome-launcher'), 
     require('karma-jasmine-html-reporter'), // click "Debug" in browser to see it 
     require('karma-htmlfile-reporter') // crashing w/ strange socket error 
    ], 

    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', 
     'node_modules/reflect-metadata/Reflect.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', 

     // 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: 'systemjs.config.js', included: false, watched: false}, 
     {pattern: 'systemjs.config.extras.js', included: false, watched: false}, 
     'test/karma-test-shim.js', 

     // transpiled application & spec code paths loaded via module imports 
     {pattern: appBase + '**/*.js', included: false, watched: true}, 
     {pattern: testBase + '**/*.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: appSrcBase + '**/*.ts', included: false, watched: false}, 
     {pattern: appBase + '**/*.js.map', included: false, watched: false}, 
     {pattern: testSrcBase + '**/*.ts', included: false, watched: false}, 
     {pattern: testBase + '**/*.js.map', included: false, watched: false} 
    ], 

    // Proxied base paths for loading assets 
    proxies: { 
     // required for component assets fetched by Angular's compiler 
     "/app/": appAssets 
    }, 

    exclude: [], 
    preprocessors: {}, 
    // disabled HtmlReporter; suddenly crashing w/ strange socket error 
    reporters: ['progress', 'kjhtml'],//'html'], 

    // HtmlReporter configuration 
    htmlReporter: { 
     // Open this file to see results in browser 
     outputFile: '_test-output/tests.html', 

     // Optional 
     pageTitle: 'Unit Tests', 
     subPageTitle: __dirname 
    }, 

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

裏面karma-test-shim.js,我做了一個線上的變化:
var builtPath = '/base/test/';

編輯: 按照docs,谷歌建議把規範的測試文件在同一文件夾中您的組件,有以下原因:

  • 這種測試很容易找到
  • 您一看便知,如果我們的應用程序的一部分,缺乏測試。
  • 附近的測試可以揭示一個部分如何在上下文中工作。
  • 當你移動源碼(不可避免)時,你記得移動
    的測試。
  • 當您重命名源文件(不可避免)時,您還記得
    以重命名測試文件。
+0

這是工作jon,感謝所需的指針。 – fruitjs