FormatTestOutput

FormatTestOutput

Provides a simplistic testing framework that integrates into seperate scripts and is based around dependent test stages, unlike frameworks like Mocha which intend for parallel testing and must be run in their own environment. section works like Mocha's describe, and it works similar to it in Mocha.

This framework should not be used in standard testing flows, as most tests should be independent. However, when testing a remote machine, chaining tests together is useful, as well as having greater control over test execution.

Test failure results in a Promise rejection, and must be manually allowed (with allowAssertion) to prevent other tests from being skipped. startBypass/resumeBypass are utilities for skipping dependent test sections after a failure.

Constructor

new FormatTestOutput()

Source:

Methods

_it() → {Promise}

Source:

Helper for FormatTestOutput#it to be used inside .then(). Given same arguments as it.

Example

Comparison to 'it'

const out = new FormatTestOutput();
out
  .it("does something first", () => true)
  .then(() => out.it("then does this", () => true))
  .then(_it("then does this", () => true)); // Same as line above, but avoids an extra wrapper function.
Returns:
Type
Promise

allowAssertion(erropt, cbopt) → {Promise}

Source:

Allows resumption of code after an assertion is thrown.

Examples

Resuming after failed assertion

const chai = require("chai"); const should = chai.should();
const out = new FormatTestOutput();
out
  .it("should fail", () => 1.should.equal(2))
  .catch(out.allowAssertion)
  .then(() => out.it("should continue", () => 2.should.equal(2)));

Calling a callback after failed assertion

const chai = require("chai"); const should = chai.should();
const out = new FormatTestOutput();
const test = "Foo Bar Baz"
out
  .it("should include 'bogus'", () => test.indexOf("bogus").should.equal(true))
  .catch(out.allowAssertion(err => console.log(out.indentLines(`Expected '${test}' to include 'bogus'.`))))
  .then(() => out.it("should continue", () => 2.should.equal(2)));
Parameters:
Name Type Attributes Description
err Error <optional>

the thrown error.

cb function <optional>

provide a custom callback to run while catching assertions.

Returns:

resolves if the given error is an AssertionError.

Type
Promise

(private) indent() → {String}

Source:

Utility for indenting sections of code.

Returns:

the current indentation to add to the beginning of messages.

Type
String

(private) indentLines(msg) → {String}

Source:

Utility for indenting a multi-line string.

Parameters:
Name Type Description
msg String

a multi-line message that should be indented.

Returns:

the given message, with every line indented.

Type
String

it(name, contents) → {Promise}

Source:

Run a single test, and logs to the console. Prints any encountered errors to the console. Works like Mocha's it.

Example

Using 'it'

const chai = require("chai"); const should = chai.should();
const out = new FormatTestOutput();
out.it("adds", () => (1 + 1).should.equal(2));
Parameters:
Name Type Description
name String

a descriptive name for this test.

contents function

the test to run. Should return a Promise if async.

Returns:

the result of running the contents.

Type
Promise

resumeBypass(code) → {function}

Source:

Resumes testing code after bypassing tests.

Parameters:
Name Type Description
code Any

the code from FormatTestOutput#startBypass that should be resumed.

Returns:

can be used inside .catch() to resume test execution

Type
function

section(name, contents) → {Promise}

Source:

Start a section of testing. Similar to Mocha's describe block. Catches any errors from inside the contents, and logs them to the console.

Example

Using 'section' with 'it'

const chai = require("chai"); const should = chai.should();
const out = new FormatTestOutput();
out.section("addition", () => {
  return it("adds numbers", () => (1 + 1).should.equal(2))
    .catch(out.allowAssertion)
    .then(out._it("continues", () => (1 + 2).should.equal(3))
    .catch(out.allowAssertion)
});
Parameters:
Name Type Description
name String

a descriptive name for this section.

contents function

the contents of this section. Must return a Promise.

Returns:

the returned value of contents.

Type
Promise

startBypass(code) → {function}

Source:

Skips a section of testing.

Examples

Using a bypass

const chai = require("chai"); const should = chai.should();
const out = new FormatTestOutput();
out
  .it("should fail", () => 1.should.equal(2))
  .catch(out.startBypass("should-fail"))
  .it("should not run", () => 1.should.equal(2))
  .catch(out.resumeBypass("should-fail"))
  .it("should run", () => 1.should.equal(1));

Using two bypasses

const text = "A simple sentance.";
const out = new FormatTestOutput();
out
  .it("should have text", () => text.length.should.be.above(0))
  .catch(out.startBypass("no-text"))
  .it("should have at least three words", () => (text.match(' ') || []).length.should.be.at.least(2))
  .catch(out.startBypass("few-words"))
  .it("test depends on having text and at least 3 words", () => text.split(" ").length.should.be.at.least(3))
  .catch(out.allowAssertion)
  .catch(out.resumeBypass("few-words"))
  .it("test needs text, but doesn't need three words", () => text.length.should.be.above(1))
  .catch(out.allowAssertion)
  .catch(out.resumeBypass("no-text"))
  .it("doesn't need text and should always run", () => 1.should.equal(1));
Parameters:
Name Type Description
code Any

a code to identify the current bypass, so multiple bypasses can be used.

Returns:

can be used inside .catch() to start skipping tests.

Type
function