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

mocha-coderoad

v0.10.4

Published

mocha test runner & reporter for atom-coderoad

Downloads

78

Readme

Mocha CodeRoad

Atom-CodeRoad Javascript test runner & reporter.

npm install mocha-coderoad

Snippets

Use atom-snippets to quickly generate test files.

Open up Atom -> Open Your Snippets. Add the contents of mocha-coderoad snippets.cson to your global snippets.cson file.

Quickly generate tests using the following snippet prefixes:

  • mochacr-f - test a function
  • mochacr-a - test an array
  • mochacr-o - test an object

Test Statements

It makes sense to write test statements using 'should', 'must' or negative statements. Remember, the failing test message will be delivered as feedback to the user.

it('should be a function')
it('must be a function')
it('isn\'t a function')

Loaders

Use a loader to run the user saved file in the context of your file. Think of a loader as a way to place the file your testing inside of your test file. Loaders are written inside of comments.

// load('user-file.js');
/* workspaceDirectory/user-file.js */

When loading files within your tutorial directory, add a second parameter of true. This can allow you to low additional data variables or functions, for example: var data = {...}.

// load('data-file.js', true)
/* workspaceDirectory/node_modules/tutorial-name/tutorial/data-file.js */

Writing Tests

Here are examples using mocha with chai's expect. See the docs.

exists

it('doesn\'t exist', function() {
    expect(target).to.not.be.undefined;
});

type

it('should be a function', function() {
    expect(target).to.be.a('function');
});

function params

it('should have two parameters', function() {
    expect(target).to.have.length(2);
});

function returns

it('should add one to the number', function () {
    expect(addOne(1)).to.equal(2);
});

equals

it('should be 42', function () {
    expect(target).to.equal(42);
});

deep equals (with objects or arrays)

it('should be {a: 42}', function () {
    expect(target).to.deep.equal({a: 42});
});

regex

it('should include the variable "count"', function () {
    var regex = new RegExp('count');
    var string = target.toString();
    expect(string.match(regex)).to.be.true;
});
it('should access the property "prop"', function () {
    var regex1 = /\.prop/;            // dot notation
    var regex2 = /\[["']prop["']\]/;  // bracket notation
    var string = target.toString();
    var result = !!string.match(regex1) || !!string.match(regex2);
    expect(result).to.be.true;
});

spies

You can use sinon or chai-spies to create a spy. See an example below:

> npm i -s chai-spies

var chai = require('chai'),
    spies = require('chai-spies');
var expect = chai.expect;
    chai.use(spies);

var spy = chai.spy.on(console, 'log');
loadJS('04-forEach.js');

it('should write "hello world" to the console', function () {
    expect(spy).to.have.been.called.with('hello world');
});

Dynamic Tests

There are situations where you might want to change data based on the current task. In this case, be careful, as all earlier tests must continue to pass.

Some variables are passed into the test runner through the node environment process.env.

See an example of dynamic data based on the task position below:

var data = [1, 2, 3];

if (process.env.TASK_POSITION === '4') {
    data = [1, 2, 3, 4];
}

Tests can also change based on the task position.

if (process.env.TASK_POSITION !== '4') {
    it('should do this', function () { ... });
} else {
    it('should to that', function () { ... });
}

See a full example.

Test Writing Tool

It's entirely possible to create a simplified testing tool that could make writing tests faster & easier.

The easiest solution would be to use editor snippets to generate a page of tests from a simple configuration object. This does not yet exist.

Config

CodeRoad tutorial configurations can be set in the package.json config.

{
  "config": {
      "testDir": "tutorial",
      "testSuffix": ".spec.js",
      "testRunner": "mocha-coderoad",
      "edit": true
  }
}

This section will likely expand in the future. For now, let's go over some of the configurations.

testDir

The relative path to the unit test directory. This makes writing unit tests paths easier.

testSuffix

The common suffix for unit tests. Also making writing unit test paths shorter.

testRunner

Specified test runner. Currently only "mocha-coderoad" is available.

edit

If set to true, Atom-CodeRoad will allow users to submit issues or submit markdown pull requests. You will also need to specify "repository" & "bugs" in your package.json file. See an example config file.