javascript unit testing with karma runner

Don't talk nonsense, start the installation
1. Install node.js first
http://nodejs.org/
2. Confirm that node.js is installed successfully . If the
node -v
command cannot be found, then you are windows server 2003 like me and need to be set Environment variables, reopen cmd
if it still doesn't work, go to the manual file and execute it first in the command line
set Path=%Path%;C:\Program Files\nodejs\
node -v

congratulations! This trick will definitely work, but this will not work, please jump off the building.
This sets the environment variables, but do not close the CMD window, and you need to reset the variables when you reopen it.
3. CMD to the project directory and install karma
http://karma-runner.github.io/0.12/intro/installation.html
cd E:\HTML5js\WebContent
npm install karma --save-dev
npm install karma-jasmine karma-chrome-launcher --save-dev
npm install -g karma-cli

When installing, sometimes it will prompt that the .dll class library cannot be found. For those of you who are sad, please install visual studio 2010. After installing visual studio, execute the npm installation command again.
4. Run the karma test command to see if it is normal
karma start

Sometimes it may not work again, then
node ./node_modules/karma/bin/karma start

5. Write a configuration file Add karma.conf.js
to the current directory
module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '.',

    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'js/*.js'
    ],

    // list of files to exclude
    exclude: [
      'client/main.js'
    ],

    preprocessors: {
    },

    // use dots reporter, as travis terminal does not support escaping sequences
    // possible values: 'dots', 'progress'
    // CLI --reporters progress
    reporters: ['progress', 'junit'],

    junitReporter: {
      // will be resolved to basePath (in the same way as files/exclude patterns)
      outputFile: 'test-results.xml'
    },

    // web server port
    // CLI --port 9876
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    // CLI --colors --no-colors
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    // CLI --log-level debug
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    // CLI --auto-watch --no-auto-watch
    autoWatch: true,

    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    // CLI --browsers Chrome,Firefox,Safari
    //browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'],
    browsers: ['IE'],

    // If browser does not capture in given timeout [ms], kill it
    // CLI --capture-timeout 5000
    captureTimeout: 20000,

    // Auto run tests on start (when browsers are captured) and exit
    // CLI --single-run --no-single-run
    singleRun: false,

    // report which specs are slower than 500ms
    // CLI --report-slower-than 500
    reportSlowerThan: 500,

    plugins: [
      'karma-jasmine',
      'karma-chrome-launcher',
      'karma-firefox-launcher',
      'karma-ie-launcher',
      'karma-junit-reporter',
      'karma-commonjs'
    ]
  });
};


6. Add js and test cases that need to be tested
Create a js directory, and create plus.js and test.js
plug.js source code in the js directory
// Some code under test
function plus(a, b) {
  return a + b;
}



test.js source code

describe('plus', function() {
  it('should pass', function() {
    expect(true).toBe(true);
  });

  it('should work', function() {
    expect(more(1, 2)).toBe(3);
  });
  
  it('should work', function() {
	    expect(more(5, 2)).toBe(9);
	  });
});




7. Execute the test Run the command
in the current directory to start the test:
karma start

If the test is unsuccessful, install the karma plugin
npm install karma-chrome-launcher karma-firefox-launcher karma-ie-launcher karma-junit-reporter karma-commonjs --save-dev

After installation, execute karma start to test the
command line to display the test results
E:\HTML5js\WebContent>node ./node_modules/karma/bin/karma start
INFO [karma]: Karma v0.12.9 server started at http://localhost:9876/
INFO [launcher]: Starting browser IE
INFO [IE 8.0.0 (Windows 7)]: Connected on socket eltz_MLU0QZG0nXCcquR with id 46
729579
..
IE 8.0.0 (Windows 7) plus should work FAILED
        Expected 7 to be 9.
IE 8.0.0 (Windows 7): Executed 3 of 3 (1 FAILED) (0.023 secs / 0.013 secs)


Automatically open IE for testing, IE interface:


8. Support IE, chrome, firefox and Jenkins continuous integration testing, please see the next episode

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326610354&siteId=291194637