automatically confirming dialogs for front-end testing with Mocha

时间:2017-12-18 06:07:12

标签: javascript jasmine mocha karma-runner

This looks like the way to auto-confirm dialogs using Jasmine:

https://coderwall.com/p/elevha/stubbing-out-confirm-dialogs-in-jasmine

that would be:

spyOn(window, 'confirm').andReturn(true);

however I cannot figure out how to do the same with Mocha...anyone know? I guess Mocha does not come with something pre-bundled for spies?

1 个答案:

答案 0 :(得分:0)

Ok so we are using Sinon with Mocha, the equivalent call is like so:

 sinon.stub(window, 'confirm').returns(true);

we used the follow karma config to load Sinon:

// Karma configuration
// Generated on Wed Oct 18 2017 09:34:40 GMT-0700 (PDT)
'use strict';
var path = require('path');
// create angular values dynamically for every karma run
require('./scripts/karma-testing/render-angular-values.js');
// run Running a single test file from command line
//KARMA_SPECS="test/unit/pages/home/controller/home-controller.spec.js" karma start karma.config.local.js
function getSpecs(specList) {
    if (specList) {
      return specList.split(',')
    } else {
      return ['test/unit/**/**/*.spec.js']
    }
  }
////////////////////////////////////////////////////////////////////////////////


module.exports = function (config) {
  var webDriverConfig = {
    hostname: 'qsghub-nprd-lnx-01',
    port: 8080
  };
  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: ['mocha', 'chai', 'sinon'],

    // list of files / patterns to load in the browser
    files: [
      //cdn
      'https://cdnjs.cloudflare.com/ajax/libs/async/2.1.5/async.min.js',
      'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js',
      'https://cdnjs.cloudflare.com/ajax/libs/node-uuid/1.4.7/uuid.js',
      'https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.0/bluebird.js',
      'https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js',
      'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js',
      'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js',
      'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js',
      'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-animate.js',
      'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js',
      'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-sanitize.js',
      'https://cdnjs.cloudflare.com/ajax/libs/angular-mocks/1.6.1/angular-mocks.js',

      // local
      './public/lib/angular-ui-router/release/angular-ui-router.js',
      './public/lib/ng-csv/build/ng-csv.js',
      './public/lib/angular-bootstrap/ui-bootstrap-tpls.js',
      './public/lib/angular-flash-alert/dist/angular-flash.min.js',
      './node_modules/karma-read-json/karma-read-json.js',
      './public/app.js',
      './public/karma-helpers/load-angular-values.js',
      './public/shared/**/*.js',
      './public/pages/**/*.js',
      './public/shared/html/**/*.html',
      './public/pages/**/view/**/*.html',
      {pattern: './test/unit/mock-json/*.json', watched: true, served: true, included: false},

      // './public/pages/home/controller/home.controller.js',
      // './public/pages/home/services/**/*.js',
      // './public/pages/home/specs/home-service.spec.js',
      //process.env.CDT_KARMA_TEST_PATH || './test/unit/**/**/*.spec.js',

    ].concat(getSpecs(process.env.KARMA_SPECS)),

    // 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: {
      './public/shared/html/**/*.html':['ng-html2js'],
      './public/shared/js/filter/*.js': ['coverage'],
      './public/shared/js/services/*.js': ['coverage'],
      './public/shared/js/directive/*.js': ['coverage'],
      './public/shared/js/directive/**/*.js': ['coverage'],
      './public/pages/**/view/**/*.html':['ng-html2js'],
      './public/pages/**/**/*.js': ['coverage']

    },

    ngHtml2JsPreprocessor: {
      // If your build process changes the path to your templates,
      // use stripPrefix and prependPrefix to adjust it.
      stripPrefix: 'public/',
      // prependPrefix: 'public/',

      // // the name of the Angular module to create
      moduleName: "templates"
  },

    coverageReporter: {
      type: 'lcov',
      dir: 'reports',
      subdir: 'coverage'
    },

    sonarQubeUnitReporter: {
      sonarQubeVersion: '5.x',
      outputFile: 'reports/gunit.xml',
      useBrowserName: false
    },

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['spec','coverage', 'sonarqubeUnit'],

    // web server port
    // host: 'localhost',
    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: true,

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    // start these browser
    browsers: ['ChromeHeadless'],
    //  process.env.USER === dftjenkins

    plugins: [
      // we don't need phantomjs, because --headless chrome removes the need for phantomjs
      'karma-chrome-launcher',
      'karma-jasmine',
      'karma-junit-reporter',
      'karma-chai',
      'karma-mocha',
      'karma-sinon',
      'karma-spec-reporter',
      'karma-read-json',
      'karma-coverage',
      'karma-sonarqube-unit-reporter',
      'karma-ng-html2js-preprocessor',
      'karma-webdriver-launcher'

    ],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: 5
  })
};