New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mocha-testdata

Package Overview
Dependencies
Maintainers
4
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mocha-testdata

Multiple test cases per mocha test

latest
Source
npmnpm
Version
1.2.0
Version published
Weekly downloads
1.3K
-4.46%
Maintainers
4
Weekly downloads
 
Created
Source

mocha-testdata

Build status "devDependencies" status

Multiple test cases per mocha test

Git repository

Changelog

Allows specifying multiple test cases for each mocha test.

This package was inspired by the given method in Pavlov.

Install

$ npm install --save-dev mocha-testdata

Usage

Here's a basic example with the BDD interface:

import * as assert from assert;
import { given } from mocha-testdata;

describe('My test suite', function () {
  given('', false, null, undefined).it('passes if value is falsey', function (value) {
    assert.ok(!value);
  });
});

And the same example with the TDD interface:

import * as assert from assert;
import { given } from mocha-testdata;

suite('My test suite', function () {
  withData('', false, null, undefined).test('passes if value is falsey', function (value) {
    assert.ok(!value);
  });
});

API

Assuming:

var testData = require('mocha-testdata');

testData.given(data)

Defines set of test data for a test case.

data

Type: Array or argument list
Default: empty array

Values to pass to the test. If the arguments themselves are arrays, they will be passed as multiple arguments to the test function. If a data item is an object, the description property is used to construct the individual test name.

Returns:

  • it: define a test case (for use with BDD interface)
  • test: define a test case (for use with TDD & qunit interfaces)

Example with multiple arguments:

import * as assert from assert;
import { given } from mocha-testdata;

suite('My test suite', function () {
  given([1, 2, 3], [3, 2, 1]).test('sum to 6', function (a, b, c) {
    assert.strictEqual(a + b + c, 6);
  });
});

testData.givenAsync(data)

Defines set of test data for an async test case.

data

Same as testData.given(data).

Returns:

  • it: define an async test case (for use with BDD interface)
  • test: define an async test case (for use with TDD & qunit interfaces)

Async test cases take a callback as their first parameter; test data are passed in subsequent parameters.

Example:

import * as assert from assert;
import { givenAsync } from mocha-testdata;

suite('My async test suite', function () {
  givenAsync([1, 2, 3], [3, 2, 1]).test('sum to 6', function (done, a, b, c) {
    doSomethingAsync(function () {
      assert.strictEqual(a + b + c, 6);
      done();
    });
  });
});

TypeScript notes

The following use-cases are currently supported by included TypeScript typings:

import { given, givenAsync } from "mocha-testdata";

// simple data
given(1, 2, 3, 4).it("One", arg => arg);
givenAsync(1, 2, 3, 4).it("Two", (done, arg) => done());

// simple predefined data
const data = [1, 2, 3, 4];
given(data).it("Three", arg => arg);
givenAsync(data).it("Four", (done, arg) => done());

// complex data
given([1, 2], [3, 4]).it("Five", (a, b) => a);
givenAsync([1, 2], [3, 4]).it("Six", (done, a, b) => done());

// complex predefined data
const complexData: { 0: number, 1: number }[] = [[1, 2], [3, 4]]; // explicit typing is required here, type inference is not good enough in TypeScript 1.8.9
given(complexData).it("Seven", (a, b) => a);
givenAsync(complexData).it("Eight", (done, a, b) => done());

// structured data
given({ a: 1, b: 2 }, { a: 1, b: 2 }).it("Nine", arg => arg.a);
givenAsync({ a: 1, b: 2 }, { a: 1, b: 2 }).it("Ten", (done, arg) => arg.a && done());

// structured predefined data
const structuredData = [{ a: 1, b: 2 }, { a: 1, b: 2 }];
given(structuredData).it("Eleven", arg => arg.a);
givenAsync(structuredData).it("Twelve", (done, arg) => arg.a && done());

Contributing

  • Clone git repository

  • npm install (will install dev dependencies needed by the next step)

  • npm start (will start a file system watcher that will re-lint JavaScript and JSON files + re-run all tests when change is detected)

  • Make changes, don't forget to add tests, submit a pull request.

License

MIT © Pandell Technology

Keywords

mocha

FAQs

Package last updated on 21 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts