Code Monkey home page Code Monkey logo

grunt-karma's Introduction

#grunt-karma Grunt plugin for Karma NOTE: this plugin requires Grunt 0.4.x

##Getting Started From the same directory as your project's Gruntfile and package.json, install this plugin with the following command:

npm install grunt-karma --save-dev

Note that even numbered minor releases follow Karma's stable channel, while odd numbers follow the unstable channel. So [email protected] goes with [email protected], while [email protected] goes with [email protected]

Once that's done, add this line to your project's Gruntfile:

grunt.loadNpmTasks('grunt-karma');

##Config Inside your Gruntfile.js file, add a section named karma, containing any number of configurations for running karma. You can either put your config in a karma config file or leave it all in your Gruntfile (recommended).

###Here's an example that points to the config file:

karma: {
  unit: {
    configFile: 'karma.conf.js'
  }
}

###Here's an example that puts the config in the Gruntfile:

karma: {
  unit: {
    options: {
      files: ['test/**/*.js']
    }
  }
}

You can override any of the config file's settings by putting them directly in the Gruntfile:

karma: {
  unit: {
    configFile: 'karma.conf.js',
    runnerPort: 9999,
    singleRun: true,
    browsers: ['PhantomJS']
  }
}

##Sharing Configs If you have multiple targets, it may be helpful to share common configuration settings between them. Grunt-karma supports this by using the options property:

karma: {
  options: {
    configFile: 'karma.conf.js',
    runnerPort: 9999,
    browsers: ['Chrome', 'Firefox']
  },
  continuous: {
    singleRun: true,
    browsers: ['PhantomJS']
  },
  dev: {
    reporters: 'dots'
  }
}

In this example the continuous and dev targets will both use the configFile and runnerPort specified in the options. But the continuous target will override the browser setting to use PhantomJS, and also run as a singleRun. The dev target will simply change the reporter to dots.

##Running tests There are three ways to run your tests with karma:

###Karma Server with Auto Runs on File Change Setting the autoWatch option to true will instruct karma to start a server and watch for changes to files, running tests automatically:

karma: {
  unit: {
    configFile: 'karma.conf.js',
    autoWatch: true
  }
}

Now run $ grunt karma

###Karma Server with Grunt Watch Many Grunt projects watch several types of files using grunt-contrib-watch. Config karma like usual (without the autoWatch option), and add background:true:

karma: {
  unit: {
    configFile: 'karma.conf.js',
    background: true
  }
}

The background option will tell grunt to run karma in a child process so it doesn't block subsequent grunt tasks.

Config your watch task to run the karma task with the :run flag. For example:

watch: {
  //run unit tests with karma (server needs to be already running)
  karma: {
    files: ['app/js/**/*.js', 'test/browser/**/*.js'],
    tasks: ['karma:unit:run'] //NOTE the :run flag
  }
},

In your terminal window run $ grunt karma:unit watch, which runs both the karma task and the watch task. Now when grunt watch detects a change to one of your watched files, it will run the tests specified in the unit target using the already running karma server. This is the preferred method for development.

###Single Run Keeping a browser window & karma server running during development is productive, but not a good solution for build processes. For that reason karma provides a "continuous integration" mode, which will launch the specified browser(s), run the tests, and close the browser(s). It also supports running tests in PhantomJS, a headless webkit browser which is great for running tests as part of a build. To run tests in continous integration mode just add the singleRun option:

karma: {
  unit: {
    configFile: 'config/karma.conf.js',
  },
  //continuous integration mode: run tests once in PhantomJS browser.
  continuous: {
    configFile: 'config/karma.conf.js',
    singleRun: true,
    browsers: ['PhantomJS']
  },
}

The build would then run grunt karma:continuous to start PhantomJS, run tests, and close PhantomJS.

##Grep / Passing Options to Karma Adapters Any cli args will be automatically parsed and sent on to adapters in the config.args property. So for example to use Mocha's useful grep feature, run grunt-karma like so:

grunt karma:dev watch --grep=mypattern

Note that adapters like karma-mocha have to support the args you're wanting to pass to them.

##License MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.