Karma以镀铬方式启动并关闭

时间:2018-05-08 10:39:14

标签: html angularjs typescript karma-jasmine

我正在测试Angular唱Jasmine和Karma。 测试似乎完美运行,我能够在Windows控制台中看到结果。但无论如何有一种方法可以使Karma保持打开状态,并且在2秒后不会自行关闭吗?

我有karma.config.js的配置:



const webpackConfig = require('../../../webpack/webpack.test.js');

const WATCH = process.argv.includes('--watch');

module.exports = (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', 'intl-shim'],

        // client:{
        //  clearContext: false // leave Jasmine Spec Runner output visible in browser
        // },
        // list of files / patterns to load in the browser
        files: [
            'spec/entry.ts'
        ],
        // list of files to exclude
        exclude: [],

        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
            'spec/entry.ts': ['webpack', 'sourcemap']
        },

        webpack: webpackConfig(WATCH),

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

        junitReporter: {
            outputFile: '../../../../target/test-results/karma/TESTS-results.xml'
        },

        notifyReporter: {
            reportEachFailure: true, // Default: false, will notify on every failed sepc
            reportSuccess: true // Default: true, will notify when a suite was successful
        },


        remapIstanbulReporter: {
            reports: { // eslint-disable-line
                'lcovonly': 'target/test-results/coverage/report-lcov/lcov.info',
                'html': 'target/test-results/coverage',
                'text-summary': null
            }
        },

        // 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: WATCH,

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

        // Ensure all browsers can run tests written in .ts files
        mime: {
            'text/x-typescript': ['ts','tsx']
        },

        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: !WATCH
    });
};




这个package.json库包含了(更多但是这里有可能感兴趣的那些):



  "devDependencies": {
  "html-loader": "0.5.0",
    "html-webpack-plugin": "2.30.1",
    "jasmine-core": "2.7.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma": "1.7.1",
    "karma-chrome-launcher": "2.2.0",
    "karma-coverage": "1.1.1",
    "karma-intl-shim": "1.0.3",
    "karma-jasmine": "1.1.0",
    "karma-junit-reporter": "1.2.0",
    "karma-notify-reporter": "1.0.1",
    "karma-phantomjs-launcher": "1.0.4",
    "karma-remap-istanbul": "0.6.0",
    "karma-sourcemap-loader": "0.3.7",
    "karma-webpack": "2.0.4",
    "merge-jsons-webpack-plugin": "1.0.11",
    "phantomjs-prebuilt": "2.1.16",
    "protractor": "~5.3.1",
    "proxy-middleware": "0.15.0",
    "rimraf": "^2.6.2",
    "source-map": "0.6.1",
    "sourcemap-istanbul-instrumenter-loader": "0.2.0",
    "style-loader": "0.18.2",
    "to-string-loader": "1.1.5",
    "tslint": "5.5.0",
    "tslint-loader": "3.5.3",
    "typescript": "2.6.2",
    ...}




现在,Karma只需启动2-3秒并再次关闭。我在配置中做了什么错误吗?

2 个答案:

答案 0 :(得分:1)

通常,CLI将运行Karma,并将singleRun设置为false,以保持浏览器打开。然而,对于Angular 6.0,引入了一个错误,其中karma运行器不再遵循此默认值,并且无论在配置中具有什么,都不会启用watch。如果你想关注它并在发布修补程序时发出通知,那么这里有一个github问题:https://github.com/angular/angular-cli/issues/10711

在此期间,您可以通过从命令行启用观看来运行它。只需使用ng test --watch true代替ng test

所以为了sumarize,你的配置没问题,@ angular / cli @ 6.0.0中只有一个错误,将在即将发布的版本中修复。在此之前,您需要手动告诉它使用--watch true标志来查看代码。

答案 1 :(得分:0)

您是在非WATCH模式下运行吗?

您有singleRun: !WATCH,我估计评估为true

如果你在WATCH模式下运行此配置,我打赌它不会关闭。

您可以无条件地设置singleRun: false