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 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-mocha-screenshot

v0.4.10

Published

grunt-mocha with screenshot added pending pull request

Downloads

5

Readme

grunt-mocha

Automatically run client-side mocha specs via grunt/mocha/PhantomJS

For a grunt task for server-side mocha tests, see grunt-mocha-test or grunt-simple-mocha

Getting Started

This plugin requires Grunt ~0.4.0. Use a 0.1.x tag of this plugin to use with Grunt ~0.3.0.

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-mocha --save-dev

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

grunt.loadNpmTasks('grunt-mocha');

Mocha task

Run this task with the grunt mocha command.

Settings

files/src

There are a number of options available. Please review the minimatch options here.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
  },
},

tasks

Type: String|Array

This defines which HTML spec files to run using PhantomJS. These are the same files you would open to run tests in a browser.

dest

Type: String
Default: undefined

Write reporter output to a file. Useful if you need a file to feed your CI bot.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    reporter: 'XUnit',
    dest: './test/output/xunit.out',
  },
},

options.run

Type: Boolean
Default: false

NOTE: This will probably default to true in 0.5.

grunt-mocha injects a script into the PhantomJS instance that loads your HTML spec files. The file sets up a reporter and listeners so the output can be output in the command line. This option will call mocha.run() after the script is injected, ensuring that the proper listeners are setup.

You may want to set this to false if your files are loaded asynchronously via AMD and call mocha.run in your own callback.

In HTML spec:

<!-- run mocha after all test are loaded -->
<script type="text/javascript" charset="utf-8">
  // Only tests run in real browser, injected script run if options.run == true
  if (navigator.userAgent.indexOf('PhantomJS') < 0) {
    mocha.run();
  }
</script>

Gruntfile:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      run: true,
    },
  },
},

options.urls

Type: Array|String
Default: []

Instead of files, hit these URLs. Usually used in conjunction with the connect task to spin up a server for testing.

connect: {
  server: {
    options: {
      port: 8888,
      base: '.',
    },
  },
},
mocha: {
  test: {
    options: {
      urls: [ 'http://localhost:8888/example/test/test2.html' ],
    },
  },
},

Then run:

grunt connect mocha

options.timeout

Type: Number
Default: 5000

PhantomJS timeout in milliseconds. If nothing happens within 5 seconds, exit.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      timeout: 10000,
    },
  },
},

options.bail

Type: Boolean
Default: false

Call grunt.warn and exit the grunt task on the first failed test. This only calls grunt.warn after the entire spec file is finished.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      bail: true,
    },
  },
},

options.log

Type: Boolean
Default: false

Print any console.log calls from PhantomJS to the command line. Only used for very quick and dirty debugging. It is highly recommended that you open the failing spec file in a browser so you can use much richer debugging tools.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      log: true,
    },
  },
},

options.logErrors

Type: Boolean
Default: false

Fail and output script errors.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      logErrors: true,
    },
  },
},

options.mocha

Type: Object

A mocha options simple object. Very few options are currently supported. Actually, I think grep is the only one.

Example:

mocha: {
  test: {
    src: ['tests/**/*.html'],
    options: {
      mocha: {
        grep: 'router*'
      }
    }
  },
},

options.reporter

Type: String
Default: 'Spec'

The reporter to use. Note: XUnit and those types of reporters should probably use the dest option.

Example:

mocha: {
  test: {
    files: ['tests/**/*.html'],
    options: {
      reporter: 'Nyan',
    }
  },
},

Custom reporter example: Example:

mocha: {
  test: {
    files: ['tests/**/*.html'],
    options {
      reporter: './path/to/custom/reporter', // included via require
    },
  },
},

Hacks

The PhantomJS -> Grunt superdimensional conduit uses alert. If you have disabled or aliased alert in your app, this won't work. I have conveniently set a global PHANTOMJS on window so you can conditionally override alert in your app.

Screenshotting

To take a screenshot:

var container = $('article')[0]; // or any DOM node
console.log(JSON.stringify({
  action: 'render', // mandatory
  filename: 'screenshots/article.png', // mandatory
  clipRect: container.getBoundingClientRect() // optional
}));

To change the resolution of the viewport:

console.log(JSON.stringify({
  action: 'viewportSize', // mandatory
  width: '960', // mandatory
  height: '960' // mandatory
}));

Examples

Vanilla JS

Option 1 (recommended)

  • Write mocha task description in grunt config using and specify run: true option (see this task's Gruntfile.js for details);
  • Check for PhantomJS userAgent in a test html file and run tests only in a real browser (see test2.html for details).

In this case you shouldn't include bridge.js (it will be included automatically) and tests will be run from bridge.js.

Option 2

Alternatively, include bridge.js from tasks/phantomjs after you include mocha.js and run mocha.setup in your HTML file. The helper will override mocha.setup if it detects PhantomJS. See test.html.

AMD

Mocha must be included via script tag in the header. There is no need to load Mocha via AMD. You may load other testing libs via AMD if that gives you a fuzzy feeling.

Example setup with AMD (advanced): https://gist.github.com/2655876

License

Copyright (c) 2013 Kelly Miyashiro Licensed under the MIT license.