2017-08-16 112 views
2

我想使用Chrome瀏覽器和構建服務器上的phantomjs在本地運行我的測試。如何配置,在karma.conf.js文件茉莉花測試使用不同瀏覽器的角度應用程序

module.exports = function (config) { 
    config.set({ 
    basePath: '', 
    frameworks: ['jasmine', '@angular/cli'], 
    plugins: [ 
     require('karma-jasmine'), 
     require('karma-chrome-launcher'), 
     require('karma-phantomjs-launcher'), 
     require('karma-jasmine-html-reporter'), 
     require('karma-coverage-istanbul-reporter'), 
     require('karma-trx-reporter'), 
     require('@angular/cli/plugins/karma') 
    ], 
    client:{ 
     clearContext: false // leave Jasmine Spec Runner output visible in browser 
    }, 
    files: [ 
     { pattern: './<%= sourceDir %>/test.ts', watched: false }, 
    ], 
    preprocessors: { 
     './<%= sourceDir %>/test.ts': ['@angular/cli'] 
    }, 
    mime: { 
     'text/x-typescript': ['ts','tsx'] 
    }, 
    coverageIstanbulReporter: { 
     reports: [ 'html', 'lcovonly' ], 
     fixWebpackSourcePaths: true 
    }, 
    angularCli: { 
     environment: 'dev' 
    }, 
    reporters: config.angularCli && config.angularCli.codeCoverage 
     ? ['progress', 'coverage-istanbul', 'trx'] 
     : ['progress', 'kjhtml', 'trx'], 
    trxReporter: { 
     outputFile: 'test-results.trx', 
     shortTestName: false 
    }, 
    port: 9876, 
    colors: true, 
    logLevel: config.LOG_INFO, 
    autoWatch: true, 
    browsers: ['Chrome'], 
    singleRun: false 
    }); 
}; 

我的生成服務器上我使用這個配置:通過運行測試NPM,

browsers: ['PhantomJs'], 
singleRun: true 

我跑我的生成服務器上測試其啓動測試。 有沒有辦法直接配置這個配置文件,或者我應該使用不同的karma.conf.js文件?我該如何使用它。或者我應該在ng測試中使用一些參數?

回答

0

我通過使用一些腳本在我的包的配置

"test": "ng test --single-run --browsers=PhantomJS", 
"test.watch": "ng test --browsers=Chrome", 
1

我所做的是使用2個不同的npm腳本來觸發測試(基本上運行karma開始),這叫做testtest-chrome。然後在karma.conf.js我module.exports這樣做過:

var ENV = process.env.npm_lifecycle_event; 
var isChrome= ENV === 'test-chrome'; 

,並在配置:

browsers: isChrome? ['Chrome'] : ['PhantomJS'] 

在你的情況,你可以在本地運行測試鉻,只是測試的構建服務器。

+0

感謝您的提示找到一個很好的和簡單的解決方案。現在我發現了一個很好的解決方案 – cpiock

+0

不客氣。您應該將您的答案標記爲已接受,因此問題已關閉。 –