7.1.0 • Published 4 months ago

riteway v7.1.0

Weekly downloads
1,698
License
MIT
Repository
github
Last release
4 months ago

RITEway

Test assertions that always supply a good bug report when they fail.

  • Readable
  • Isolated/Integrated
  • Thorough
  • Explicit

RITEway forces you to write Readable, Isolated, and Explicit tests, because that's the only way you can use the API. It also makes it easier to be thorough by making test assertions so simple that you'll want to write more of them.

There are 5 questions every unit test must answer. RITEWay forces you to answer them.

  1. What is the unit under test (module, function, class, whatever)?
  2. What should it do? (Prose description)
  3. What was the actual output?
  4. What was the expected output?
  5. How do you reproduce the failure?

Installing

npm install --save-dev riteway

and add an npm command in your package.json:

"test": "riteway test/**/*-test.js",

so that you can run tests with: npm test. Riteway also supports full TAPE-compatible usage syntax, so you can have an advanced entry that looks something like the following:

"test": "nyc riteway test/**/*-rt.js | tap-nirvana",

In this latter case, riteway run is managed by popular nyc module that generates test coverage stats/reports and the on-screen output is piped through an advanced TAPE formatter tap-nirvana that adds color coding, source line identification and advanced diff-ing capabilities.

Example Usage

import { describe, Try } from 'riteway';

// a function to test
const sum = (...args) => {
  if (args.some(v => Number.isNaN(v))) throw new TypeError('NaN');
  return args.reduce((acc, n) => acc + n, 0);
};

describe('sum()', async assert => {
  const should = 'return the correct sum';

  assert({
    given: 'no arguments',
    should: 'return 0',
    actual: sum(),
    expected: 0
  });

  assert({
    given: 'zero',
    should,
    actual: sum(2, 0),
    expected: 2
  });

  assert({
    given: 'negative numbers',
    should,
    actual: sum(1, -4),
    expected: -3
  });

  assert({
    given: 'NaN',
    should: 'throw',
    actual: Try(sum, 1, NaN),
    expected: new TypeError('NaN')
  });
});

Output

RITEway produces standard TAP output, so it's easy to integrate with just about any test formatter and reporting tool. (TAP is a well established standard with hundreds (thousands?) of integrations).

TAP version 13
# sum()
ok 1 Given no arguments: should return 0
ok 2 Given zero: should return the correct sum
ok 3 Given negative numbers: should return the correct sum
ok 4 Given NaN: should throw

1..4
# tests 4
# pass  4

# ok

Prefer colorful output? No problem. The standard TAP output has you covered. You can run it through any TAP formatter you like:

npm install -g tap-color
npm test | tap-color

Colorized output

API

describe

describe = (unit: String, cb: TestFunction) => Void

Describe takes a prose description of the unit under test (function, module, whatever), and a callback function (cb: TestFunction). The callback function should be an async function so that the test can automatically complete when it reaches the end. RITEWay assumes that all tests are asynchronous. Async functions automatically return a promise in JavaScript, so RITEWay knows when to end each test.

describe.only

describe.only = (unit: String, cb: TestFunction) => Void

Like Describe, but don't run any other tests in the test suite. See test.only

describe.skip

describe.skip = (unit: String, cb: TestFunction) => Void

Skip running this test. See test.skip

TestFunction

TestFunction = assert => Promise | Void

The TestFunction is a user-defined function which takes assert() and should return a promise. If you supply an async function, it will return a promise automatically. If you don't, you'll need to explicitly return a promise.

Failure to resolve the TestFunction promise will cause an error telling you that your test exited without ending. Usually, the fix is to add async to your TestFunction signature, e.g.:

describe('sum()', async assert => {
  /* test goes here */
});

assert

assert = ({
  given = Any,
  should = '',
  actual: Any,
  expected: Any
} = {}) => Void, throws

The assert function is the function you call to make your assertions. It takes prose descriptions for given and should (which should be strings), and invokes the test harness to evaluate the pass/fail status of the test. Unless you're using a custom test harness, assertion failures will cause a test failure report and an error exit status.

createStream

createStream = ({ objectMode: Boolean }) => NodeStream

Create a stream of output, bypassing the default output stream that writes messages to console.log(). By default the stream will be a text stream of TAP output, but you can get an object stream instead by setting opts.objectMode to true.

import { describe, createStream } from 'riteway';

createStream({ objectMode: true }).on('data', function (row) {
    console.log(JSON.stringify(row))
});

describe('foo', async assert => {
  /* your tests here */
});
8.0.0-RC3

4 months ago

8.0.0-RC2

4 months ago

8.0.0-RC4

4 months ago

8.0.0-RC1

4 months ago

7.1.0

4 months ago

7.0.0

2 years ago

7.0.0-rc.0

2 years ago

6.3.0

3 years ago

6.3.1

3 years ago

6.2.1

4 years ago

6.2.0

4 years ago

6.1.2

4 years ago

6.1.1

5 years ago

6.1.0

5 years ago

6.0.3

5 years ago

6.0.2

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

5.0.0-rc1

5 years ago

4.0.1

5 years ago

4.0.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago