Install

Mokamok can be installed with npm:

$ npm install --save-dev mokamok

Getting Started

Running the first test

Create a directory called --tests--, and add a test file increment.spec.js:

import increment from '../increment';

describe("increment.js", () => {

    it("should increment the value", () => {
        expect(increment(7)).to.equal(8);
    });

});

Let’s implement the increment.js module:

export default function (v) {
    return v + 1;
}

Modify the package.json file:

"scripts": {
    "test": "mokamok"
},

Run the test:

$ npm test

Watch mode

Watch mode makes it easy for developers to see the effects of code changes immediately. Watch mode cna be initiated with the --watch or -w command line option. Mokamok will run the test suit every time wen any of the source or test files is modified.

Coverage report

Mokamok can build full code coverage report with the --coverage or -c command line options or coverage: true property in the .mokamokrc file. The test runner will print the summary of the report and generate the full report into the coverage directory. The coverage report is not generated in watch mode.

Using mocks

Mokamok makes it easy to break the link between the tested module and it’s dependencies. In order to mock a module, use the mokamok.mock() api call before importing the module:

mokamok.mock('depndency-module');

import myModule from 'my-module';

this will replace all exports in the dependency module with a stub. When the module exports a class then all instance of the class will be mocked.

With the the mock function you can provide your own mock object as the second parameter:

mokamok.mock('depndency-module', {
    __esModule: true,
    default: () => 'VALUE',
    otherFunction: () => 'OTHER_VALUE'
});

Mokamok is based on Sinon so all mocked dependencies can be tested easily:

mokamok.mock('depndency-module');

import myModule from 'my-module';
import depndencyModule from 'depndency-module';

it("should call the do() function", function () {
    const param = Symbol();
    myModule.exec(param);
    expect(depndencyModule.do).to.be.calledWithExactly(param);
});

Mokamok resets every mocks after each tests. For more information on how to use Sinon check the documentation: http://sinonjs.org/docs/

Automock

Mokamok can be configured to automatically mock every module with the --automock or -m command line options or automock: true property in the .mokamokrc file. When the automock functionality is turned on, the actual tested file need to be unmocked before imported:

mokamok.unmock('my-module');

import myModule from 'my-module';

In the example above every module but my-module.js will be mocked automatically.

Jsdom support

Jsdom support can be enabled with the --jsdom or -j command line options or jsdom: true property in the .mokamokrc file. With jsdom turned on Mokamok makes the browser environment available in nodejs. Mokamok resets jsdom after every test.

Testing React

Setup

Install the mokamok-react package:

npm install --save-dev mokamok-react

Rendering react components

React component can be rendered with the mokamok.render() API call. This function will render all nested components.

import React from 'react';
import Test from '../test';

let node;

beforeEach(function () {
    node = mokamok.render(<Test text="React" />);
});

Shallow rendering

Shallow rendering lets you render a component “one level deep”. With a shallow rendered node you can test the top level and the child components but not the children of the

import React from 'react';
import Test from '../test';

let node;

beforeEach(function () {
    node = mokamok.shallow(<Test text="React" />);
});

Testing the components

You can find elements in rendered component with full jQuery selectors and assert them with cahi-jquery. Shallow rendered component can be tested in the exact same way as full rendered ones. The only difference is that on the external components only the properties are accessible not the children.

import React from 'react';
import Test from '../test';

it("should render components", () => {
    const node = mokamok.render(<Test text="React" />);
    expect(node.find('>span')).to.have.text('React');
    expect(node.find('a:first-child')).to.have
        .attr('href', 'http://github.com/ggyorfi/mokamok');
});

API Docs

mokamok.mock(moduleName, [mock])

Marks the module to be mocked (see: Using mocks)

Parameters:

  • moduleName : String - the name of the module
  • mock : Object (optional) - the mock object

mokamok.unmock(moduleName)

Marks the module to not be mocked (see: Using mocks)

Parameters:

  • moduleName : String - the name of the module

sandbox.mock(obj)

Sandboxed version of the sinon.mock() api function. (see: Mocks and Sandboxes).

sandbox.spy([myFunc])

Sandboxed version of the sinon.spy() api call. (see: Test spies and Sandboxes).

sandbox.stub([object, “method”, [func]])

Sandboxed version of the sinon.stub() api call. (see: Test stubs and Sandboxes).

sandbox.render(markup)

Renders a React component to full DOM representation. (see: Testing React)

Parameters:

  • markup : JSX - the rendered React component

sandbox.shallow(markup)

Renders a React component “one level deep”. (see: Testing React)

Parameters:

  • markup : JSX - the rendered React component

sandbox.stubEventHandlers(Component)

Stubs all event handler (functions starting with ‘on’ or ‘handle’ and followed by an upper case letter in a React component.

Parameters:

  • Component : React.Component - the component class

Command line options

Usage: index [options]

  Options:

    -h, --help                   output usage information
    -v, --version                output the version number
    -m, --automock               mock every module in the project automatically
    -j, --jsdom                  use jsdom
    -w, --watch                  watch for file changes
    -c, --coverage               generate a code coverage report
    -d, --test-directory <name>  test directory name
    -B, --no-babel               disable babel support