运行 单元测试 karma 脚本后的 jenkins 管道不停止

jenkins pipeline after run unit test karma script do not stop

我创建了 un jenkins 管道,其中 运行 一个带有 nodeJs、karma 和 xvfb 的 docker 容器,用于 运行 单元测试。 测试成功通过,但脚本永远不会停止,即使在 karma 配置中使用 captureTimeout: 60000 和 singleRun: true

我哪里错了

[33m05 04 2017 16:01:54.227:WARN [karma]: [39mNo captured browser, open http://localhost:9876/
[32m05 04 2017 16:01:54.260:INFO [karma]: [39mKarma v1.3.0 server started at http://localhost:9876/
[32m05 04 2017 16:01:54.262:INFO [launcher]: [39mLaunching browser Chrome with unlimited concurrency
[32m05 04 2017 16:01:54.282:INFO [launcher]: [39mStarting browser Chrome
[32m05 04 2017 16:01:59.120:INFO [Chrome 57.0.2987 (Linux 0.0.0)]: [39mConnected on socket /#0Rk4DaJRdDHKCHNvAAAA with id 66659056
Chrome 57.0.2987 (Linux 0.0.0): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
[1A[2KChrome 57.0.2987 (Linux 0.0.0): Executed 1 of 2 SUCCESS (0 secs / 0.99 secs)
[1A[2KLOG: 'MAIN'
Chrome 57.0.2987 (Linux 0.0.0): Executed 1 of 2 SUCCESS (0 secs / 0.99 secs)
[1A[2KChrome 57.0.2987 (Linux 0.0.0): Executed 2 of 2 SUCCESS (0 secs / 1.269 secs)
[1A[2KChrome 57.0.2987 (Linux 0.0.0): Executed 2 of 2 SUCCESS (1.319 secs / 1.269 secs)

管道:

        def karma = docker.image('trion/ng-cli-karma')
        karma.pull()

        try {

            karma.run(' -u $(id -u) -v ${WORKSPACE}:/app trion/ng-cli-karma ')
            karma.inside {
                sh 'npm install'

                try {
                    sh('ng test')
                }catch(err) {
                    sh 'echo TEST FAILED'
                    step([$class: 'JUnitResultArchiver', testResults: 'report/*.xml', healthScaleFactor: 1.0])
                    throw err
                }
                sh 'echo DO SOMETHING ELSE AFTER TEST'
            }
            sh 'ls -al '
        } catch(err) {
            sh 'echo RUN DOCKER FAILED'
            throw err
        }

业力会议:

module.exports = function (config) {
    config.set({
        mime: { 'text/x-typescript': ['ts','tsx'] },
        basePath: '',
        frameworks: ['jasmine', 'angular-cli'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-remap-istanbul'),
            require('angular-cli/plugins/karma'),
            require('karma-junit-reporter')
        ],
        files: [
            { pattern: './src/test.ts', watched: false }
        ],
        preprocessors: {
            './src/test.ts': ['angular-cli']
        },
        remapIstanbulReporter: {
            reports: {
                html: 'coverage',
                lcovonly: './coverage/coverage.lcov'
            }
        },
        angularCli: {
            config: './angular-cli.json',
            environment: 'dev'
        },
        reporters: config.angularCli && config.angularCli.codeCoverage
            ? ['progress', 'karma-remap-istanbul']
            : ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        customLaunchers: {
            Chrome_without_sandbox: {
                base: 'Chrome',
                flags: ['--no-sandbox'] // with sandbox it fails under Docker
            }
        },
        junitReporter: {
            outputDir: './report', // results will be saved as $outputDir/$browserName.xml
            outputFile: 'report.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
            suite: '', // suite will become the package name attribute in xml testsuite element
            useBrowserName: false, // add browser name to report and classes names
            nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
            classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
            properties: {} // key value pair of properties to add to the <properties> section of the report
        },
        reporters: config.angularCli && config.angularCli.codeCoverage
            ? ['progress', 'karma-remap-istanbul', 'junit']
            : ['progress', 'junit'],
        captureTimeout: 60000,
        singleRun: true
    });
};

编辑:2017 年 7 月 4 日 没有人可以帮助我或者可能我不清楚,我的问题是当我 运行 我的詹金斯管道 'the Run unit test' 步骤从未完成(cf 截图)并且我无法检查我的测试是否通过或不启动下一步。我需要帮助才能知道在 karma 测试完成后如何使用命令行或其他方式停止它。

我尝试使用 karma 配置(captureTimeout:60000,singleRun:true)来实现,但什么也没发生

我解决了这个问题,之前在我的 Jenkinsfile 中,我使用命令 sh('ng test') 来 运行 测试,但不知道后台发生了什么。

所以我直接用了karma命令

sh ('./node_modules/karma/bin/karma start karma.conf.js')

我假设 karma 正在观察对开发机器有利但对 CI 构建不利的变化。如果你想避免改变 karma 配置,你可以将 --watch false 作为参数传递给 ng test。 (虽然你有 singleRun: true 我不确定 autoWatch: true 是否会有更高的优先级。)

我建议您使用 docker 容器来执行完整的构建而不是单个 docker-运行 命令。 Jenkins 为 docker 容器内的 运行 命令提供了一个很好的抽象:

docker.image('trion/ng-cli-karma').inside {
        stage ('load npm dependencies') {
            echo 'Load npm dependencies'
            sh 'npm install'
        }
        stage ('build') {
            echo "building"
            sh 'npm run build'
        }
        stage ('unit test') {
          sh 'ng test --progress false --watch false'
          echo 'generate test report **/dist/test-reports/*.xml'
          junit allowEmptyResults: false, testResults: '**/test-results.xml'
          echo 'end test & coverage'
        }
   ...