首页 文章

测试用angular4 mocha业力重复

提问于
浏览
0

问题/问题

我已经将angular-cli 1.0.0生成的基本项目从使用jasmine转换为使用mocha . Why as I complete the conversion to mocha, do all the tests run twice? 有趣的是,当我们使用wallabyjs runner或降级的angular2运行相同的测试/代码时,测试只运行一次 .

为了从头开始重现,我选择了第3步,选择了第4步 . 不幸的是,涉及到很多代码,所以我提供了一个存储库来演示和遵循每个步骤的命令,可以在步骤细节中找到 .

步骤1使用angular-cli创建项目

  • ng new someBlankProject

  • cd someBlankProject

  • ng test 预期结果3次测试运行

细节

  • 配置

  • angular-cli angular4业力茉莉

  • angular-cli项目jasmine karma测试结果

  • only 3 tests in source

  • command to reproduce

  • git clone git@github.com:arranbartish/angular-cli-seed.git 01-fresh-project; cd 01-fresh-project; git checkout 2bf12b577173085344a86e37726d9f7ece930c77;npm install;npm run test -- --single-run=true

步骤2验证茉莉花测试在本地和CI上使用karma和wallaby运行

  • 介绍diff包括CI和wallabyjs亚军

  • npm install 来解决依赖关系

  • ng test 预期结果3次测试运行

  • 用wallabyjs确认 No failing tests, 3 passing

细节

  • 配置

  • angular-cli angular4业力茉莉

  • angular-cli项目jasmine karma测试结果

  • 传递构建3 of 3 tests passing

  • only 3 tests in source

  • command to reproduce

  • git clone git@github.com:arranbartish/angular-cli-seed.git 02-add-wallaby; cd 02-add-wallaby; git checkout 0bb96884d0e71f286d0b4fedc0dcafd20dc9d2b1;npm install;npm run test -- --single-run=true

  • wallabyjs结果

  • No failing tests, 3 passing

步骤3介绍mocha并将规格从jasmine转换为mocha

  • 介绍diff转换为mocha

  • npm prune;npm install 清理并解决依赖关系

  • ng test 预期结果 3 tests run 实际结果 6 tests run

  • Chromeium 53.0.2785 (Ubuntu 0.0.0): Executed 6 of 3 SUCCESS (0.347 secs / 0.042 secs)

  • 用wallabyjs确认 No failing tests, 3 passing

细节

  • 配置

  • angular-cli angular4 karma mocha

  • angular-cli项目mocha业力测试结果

  • "Failed" build 6 of 3 tests passing

  • only 3 tests in source

  • command to reproduce

  • git clone git@github.com:arranbartish/angular-cli-seed.git 03-converted-mocha; cd 03-converted-mocha; git checkout 0a9ed8804e15c451ff0d67ebd2d38980d54f9763;npm install;npm run test -- --single-run=true

  • wallabyjs结果

  • No failing tests, 3 passing

可选观察

我观察到的最奇怪的事情之一是使用角度2不具有相同的行为!

步骤4将角度从4降级为2

  • 引入diff降级angular4

  • npm prune;npm install 清理并解决依赖关系

  • ng test 预期结果3次测试运行

  • 用wallabyjs确认 No failing tests, 3 passing

步骤细节

  • 配置

  • angular-cli angular2 karma mocha

  • angular-cli项目mocha karma angular2测试结果

  • 传递构建3 of 3 tests passing

  • only 3 tests in source

  • command to reproduce

  • git clone git@github.com:arranbartish/angular-cli-seed.git 04-downgrade-angular; cd 04-downgrade-angular; git checkout 0e6c2811b9aab722fcc382e4676d97c089ad1f91;npm install;npm run test -- --single-run=true

  • wallabyjs结果

  • No failing tests, 3 passing

1 回答

  • 0

    业力的框架配置是问题

    因为wallabyjs正如预期的那样工作,所以我花了很多时间探索业力和业力摩卡,重点关注 karma.conf.js

    破碎的karma.conf.js

    // Karma configuration file, see link for more information
    // https://karma-runner.github.io/0.13/config/configuration-file.html
    
    module.exports = function (config) {
      config.set({
        basePath: '',
        frameworks: ['mocha', 'chai', 'sinon-chai', '@angular/cli'],
        plugins: [
          require('karma-mocha'),
          require('karma-chai'),
          require('karma-sinon'),
          require('karma-sinon-chai'),
          require('karma-chrome-launcher'),
          require('karma-phantomjs-launcher'),
          require('karma-mocha-reporter'),
          require('karma-coverage-istanbul-reporter'),
          require('karma-istanbul-threshold'),
          require('@angular/cli/plugins/karma')
        ],
        client:{
          clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        files: [
          { pattern: 'node_modules/sinon/pkg/sinon.js', instrument: false },
          { pattern: 'node_modules/chai/chai.js', instrument: false },
          { pattern: 'node_modules/sinon-chai/lib/sinon-chai.js', instrument: false },
          { pattern: './src/test.ts', watched: false }
        ],
        preprocessors: {
          './src/test.ts': ['@angular/cli']
        },
        mime: {
          'text/x-typescript': ['ts','tsx']
        },
        coverageIstanbulReporter: {
          reports: [ 'html', 'lcovonly', 'json' ],
          fixWebpackSourcePaths: true
        },
        istanbulThresholdReporter: {
          src: 'coverage/coverage-final.json',
          reporters: ['text'],
          thresholds: {
            global: {
              statements: 95.01,
              branches: 75.59,
              lines: 91.89,
              functions: 89.23
            },
            each: {
              statements: 75.76,
              branches: 33.33,
              lines: 75,
              functions: 41.67
            }
          }
        },
        angularCli: {
          environment: 'dev'
        },
        reporters: config.angularCli && config.angularCli.codeCoverage
                  ? ['progress', 'coverage-istanbul', 'istanbul-threshold']
                  : ['progress', 'mocha'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false
      });
    };
    

    我采用的方法是“尝试直到它工作”的最佳实践,这转化为我逐行删除部分配置以查看它是否重要并查看它是否改变了行为 .

    分辨率

    具体来说,修复是从 frameworks 数组中删除 sinon-chai 和可选的 chai .

    frameworks: ['mocha', 'chai', 'sinon-chai', '@angular/cli'],

    frameworks: ['mocha', '@angular/cli'],

    资源

    工作代码可以在this commit找到_1170948_使用angular-cli / karma / mocha / angular4

    Travis构建结果

    Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 0 of 3 SUCCESS (0 secs / 0 secs)
    Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 1 of 3 SUCCESS (0 secs / 0.062 secs)
    Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 2 of 3 SUCCESS (0 secs / 0.079 secs)
    Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 3 of 3 SUCCESS (0 secs / 0.086 secs)
    Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 3 of 3 SUCCESS (0.32 secs / 0.086 secs)
    

相关问题