TestRunner

Introduced in 4.0.0

Stability: 2 - Stable

const TestRunner = require('kado/lib/TestRunner')

The TestRunner library provides a complete execution and reporting suite suitable for testing any javascript function, library, or application.

Features of TestRunner

Motivations

While in the process of writing clean ES6 code for our libraries and removing dependencies we realized that our existing test framework was heavy and contained over 100 dependencies. We could not understand the need for so much code to do testing. We also wanted to reduce the time needed to invoke tests. Our philosophy the faster the tests the more likely the developer is to use them. Furthermore, asking the developer to depend on the tests by implementing test driven development, the tests must then be of equal or greater performance than production.

Usage

The use pattern of TestRunner is a simplistic repetitive pattern. Here is an example of the usage:

const runner = require('kado/lib/TestRunner').getInstance('test')
const { expect } = require('kado/lib/Assert')
runner.suite('MySuite', (it) => {
  it.suite('SubSuite', (it) => {
    it('should pend this test')
    it('should pass this test', () => { return true })
    it('should fail this test', () => { return false })
    it('should pass an assert', () => { expect.eq(true) })
  })
})
const someOptions = {hideFailed: true}
runner.execute(someOptions)

Here the test runner can be seen getting setup. Then a primary suite MySuite is created. Next, a subordinate suite is created by called it.suite() which matches the original runner.suite() call. Tests are defined by calling it(testName,testFunction) where the testName is a string defining the test specification, testFunction is a function that returns true or false or a Promise that resolves to true or false. Assertions are made during the test by any outside library that throws errors.

Class: TestReporter

TestReporter.constructor(name, options)

TestReporter.out(msg, indentLevel)

TestReporter.color(str, color, nextColor)

TestReporter.colorRunner()

TestReporter.colorSuite()

TestReporter.colorError()

TestReporter.colorWarn()

TestReporter.colorOk()

TestReporter.colorPending()

TestReporter.colorInfo()

TestReporter.colorDefault()

TestReporter.duration(duration)

Rules for duration color:

TestReporter.printTitle(suite)

TestReporter.testComplete(test, verb, extra)

TestReporter.suiteComplete(suite)

TestReporter.printRunnerTitle(name, options)

TestReporter.allFinished(duration, suiteCount, testsPassing, testsFailing, testsPending)

TestReporter.failedTests(failed)

Class: TestSuite

static TestSuite.executeHook(hookKeys, hookObj, reporter)

TestSuite.constructor(name)

TestSuite.setReporter(reporter)

At instantiation time the runner will setup a default reporter, using this method will customize that reporter.

TestSuite.before(fn)

Note: this method can be called multiple times to register multiple hooks.

TestSuite.beforeEach(fn)

Note: this method can be called multiple times to register multiple hooks.

TestSuite.afterEach(fn)

Note: this method can be called multiple times to register multiple hooks.

TestSuite.after(fn)

Note: this method can be called multiple times to register multiple hooks.

TestSuite.suite(name, suiteWrapper)

The suites made from this method are hierarchy linked to the suite this method is called from.

TestSuite.it(name, test, flags)

TestSuite.executeSuite(suiteKeys, runner, testResult, options)

TestSuite.executeTest(testKeys, runner, testResult)

TestSuite.execute(runner, testResult, options)

Class: TestRunner

static TestRunner.runTest(name, test)

The promise returns an {object} with the following properties:

static TestRunner.testResult(result, reporter)

static TestRunner.getInstance(name)

TestRunner.constructor(name)

TestRunner.setName(name)

Sets the suite to a new name

TestRunner.setReporter(reporter)

At instantiation time the runner will setup a default reporter, using this method will customize that reporter.

TestRunner.before(fn)

Note: this method can be called multiple times to register multiple hooks.

TestRunner.after(fn)

Note: this method can be called multiple times to register multiple hooks.

TestRunner.suite(name, suiteWrapper)

The suites made from this method are hierarchy linked to this runner.

TestRunner.test(name, test, flags)

TestRunner.executeSuite(suiteKeys, options)

TestRunner.executeTest(testKeys)

TestRunner.execute(options)