npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

grunt-server-mocha

v0.1.1

Published

A Grunt plugin for running server-side Mocha tests.

Readme

grunt-server-mocha

A Grunt plugin for running server-side Mocha tests. This is a fork of Josh Davis's grunt-cafe-mocha plugin that's been translated into CoffeeScript and supports testing RequireJS modules.

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-server-mocha --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-server-mocha');

The "mocha" task

Overview

In your project's Gruntfile, add a section named mocha to the data object passed into grunt.initConfig().

grunt.initConfig({
  "mocha-server": {
    testThis: {
        src: 'test/this/**/*.js',
        options: {
            ui: 'bdd',
            require: [
                'should',
            ],
        },
    },

    /// Any number of tests here...

    testThat: {
        src: 'test/that/*.js',
        options: {
            ui: 'tdd',
            growl: true,
            reporter: 'nyan',
        },
    },

    testRequireJS: {
        src: 'test/rjs/**/*.test.js',
        options: {
            rjsConfig: {
                baseUrl: "./src",
                paths: {
                    foo: "bar/v1.0"
                }
            }
        }
    }
  },
})

Options

options.require

Type: List

Default value: []

A list of modules to load before running the tests.

options.reporter

Type: String

Default value: 'list'

A string value to pick which reporter to use when testing. To see a list of all the reporters, check here.

If using the reporter html-cov or json-cov, take a special look at the coverage option.

options.ui

Type: String

Default value: 'bdd'

A string value to pick which type of user-interface to use. The options are bdd, tdd, or exports. Read more about interfaces here.

options.grep

Type: String to be turned into a RegExp

Default value: .*

A string value to run tests that only match the given pattern. For example, if the Mocha test file has the tests: test1, test2, test3, test4, if options.grep = 'test[12]', it will only match the first two tests and run them.

options.invert

Type: Boolean

Default value: false

Either true or false that will either match invert the matching. Using the example from the section for options.grep, setting this option to true would result in the last two tests being ran, but not the first two.

options.timeout

Type: Integer

Default value: 2000

The tiemout time for a test-case in milliseconds.

options.slow

Type: Integer

Default value: 2000

Threshold for a "slow" test in milliseconds.

options.colors

Type: Boolean

Default value: undefined

True forces the enabling of colors while false forces the disabling of colors.

options.growl

Type: Boolean

Default value: false

Enable Growl/OS X (10.8) notification system on test completion.

options.bail

Type: Boolean

Default value: false

Bail after the first test failure.

options.globals

Type: List

Default value: []

A list of globals names.

options.ignoreLeaks

Type: Boolean

Default value: false

Ignore global variable leaks

options.coverage

Type: Boolean | Object

Default value: false

Turns on the coverage feature. If options.coverage is set to true, the following defaults are used:

{
    output: 'coverage.html',
    env: 'COV'
}

To override the defaults, just pass in an object like so:

coverage: {
    output: 'coverageTwo.html',
    env: 'ENHANCED_COVERAGE',
}

The output option is a path to where the coverage output will be saved to.

The env option is the name of the process.env variable to set to a truthy value. For example, if coverage.env = 'ENHANCED_COVERAGE' then in your project, process.env['ENHANCED_COVERAGE'] will be truthy.

Check out the Coverage Example for more details.

options.rjsConfig

Type: Object

Default value: {}

If specified, will use the given RequireJS configuration to load the test files picked up in the src paths as RequireJS modules.

Usage Examples

Basic Behavioral-driven Development

grunt.initConfig({
  "mocha-server": {
    src: 'test/**/*.js',
    options: {
        ui: 'bdd',
    },
  },
})

Basic Test-driven Development

grunt.initConfig({
    "mocha-server": {
        src: 'test/**/*.js',
        options: {
            ui: 'tdd',
        },
    },
});

Require More Modules

grunt.initConfig({
    "mocha-server": {
        options: {
            require: ['should', 'something', 'else', 'here'];
        },
        src: 'test/**/*.js'
    },
});

More Tests

grunt.initConfig({
    "mocha-server": {
        foo: {
            src: 'test/foo/*.js',
            options: {
                ui: 'tdd',
            },
        },

        bar: {
            src: 'test/bar/*.js',
            options: {
                ui: 'bdd',
            },
        },
    },
});

Coverage Example

The complete example of this is included in the example/ directory.

grunt.initConfig({
    "mocha-server": {
        // Setting 'coverage' option to true, using defaults
        coverageOne: {
            src: 'test/*.js',
            options: {
                ui: 'bdd',
                reporter: 'html-cov',
                coverage: true,
                require: [
                    'should',
                ],
            },
        },

        // Setting 'coverage' option to an object, overriding defaults
        coverageTwo: {
            src: 'test/*.js',
            options: {
                ui: 'bdd',
                reporter: 'html-cov',
                coverage: {
                    output: 'coverageTwo.html',
                    env: 'ENHANCED_COVERAGE',
                },
                require: [
                    'should',
                ],
            },
        }
    },
});

Contributing

Feel free to fork it and add as you please. If you add a particularly nice feature, send me a pull request. I'd love to improve it.

Todo List

  • Support the --compilers option in Mocha
  • Support the watching of files for changes