testing-patterns v1.1.0
testing-patterns
let ping = require('ping');
let AssertionTest = require('testing-patterns/AssertionTest');
const PingTest = AssertionTest()
.describe('can ping internet')
.tag('ping', 'network')
.setup(
// build our setup
(next) => {
const setup = {};
setup.testHosts = [ 'google.com', 'microsoft.com', 'yahoo.com' ];
next(null, setup);
}
)
.prepare(
// run test with first host
(next, setup) => {
const host = setup.testHosts[0];
next(null, host);
}
)
.execute(
(next, host) => ping.sys.probe(
host,
(isAlive, error) => next(error, isAlive)
)
)
.verify(
// verify no error was thrown
(next, { setup, request, result, error }) => next(error),
// verify result is true
(next, { setup, request, result, error }) => next(
result !== true ? new Error(`could not ping host ${request}`) : null
)
)
.teardown(
// nothing to teardown
(next, { setup, request, result, error }) => next()
)
.build();
test( (error) => console.log('test done') );
testing-patterns
is a collection of utilities to help you build common test patterns.
API
testing-patterns : object
Kind: global namespace
- testing-patterns : object
- .AssertionTest
- new AssertionTest()
- assertionTest.describe(description) ⇒ AssertionTest
- assertionTest.tag(...tags) ⇒ AssertionTest
- assertionTest.setup(task) ⇒ AssertionTest
- assertionTest.prepare(task) ⇒ AssertionTest
- assertionTest.execute(task) ⇒ AssertionTest
- assertionTest.verify(...tasks) ⇒ AssertionTest
- assertionTest.teardown(task) ⇒ AssertionTest
- assertionTest.build() ⇒ function
- .AssertionTest
testing-patterns.AssertionTest
Kind: static class of testing-patterns
- .AssertionTest
- new AssertionTest()
- assertionTest.describe(description) ⇒ AssertionTest
- assertionTest.tag(...tags) ⇒ AssertionTest
- assertionTest.setup(task) ⇒ AssertionTest
- assertionTest.prepare(task) ⇒ AssertionTest
- assertionTest.execute(task) ⇒ AssertionTest
- assertionTest.verify(...tasks) ⇒ AssertionTest
- assertionTest.teardown(task) ⇒ AssertionTest
- assertionTest.build() ⇒ function
new AssertionTest()
const PingTest = AssertionTest()
.describe('can ping internet')
.tag('ping', 'network')
.setup(
// build our setup
(next) => {
const setup = {};
setup.testHosts = [ 'google.com', 'microsoft.com', 'yahoo.com' ];
next(null, setup);
}
)
.prepare(
// run test with first host
(next, setup) => {
const host = setup.testHosts[0];
next(null, host);
}
)
.execute(
(next, host) => ping.sys.probe(
host,
(isAlive, error) => next(error, isAlive)
)
)
.verify(
// verify no error was thrown
(next, { setup, request, result, error }) => next(error),
// verify result is true
(next, { setup, request, result, error }) => next(
result !== true ? new Error(`could not ping host ${request}`) : null
)
)
.teardown(
// nothing to teardown
(next, { setup, request, result, error }) => next()
)
.build();
test( () => console.log('test done') );
Constructor for an AssertionTest builder.
assertionTest.describe(description) ⇒ AssertionTest
AssertionTest#describe
lets you set a description for a test case.
This description is part of the label attached to the test case when built.
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- description string - a string label describing the test case
assertionTest.tag(...tags) ⇒ AssertionTest
AssertionTest#tag
lets you add any number of tags to a test case label.
These tags can help select a slice of your test collection to run or analyze.
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- ...tags string - any number of tags to be appended to the label
assertionTest.setup(task) ⇒ AssertionTest
AssertionTest#setup
gives you a hook to build test fixtures before execution.
This is the first step that runs in a test.
setup
is a separate step from prepare
because you often want to use
a common setup function to build test fixtures for multiple tests.
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- task function - a setup task function - should return a setup object
assertionTest.prepare(task) ⇒ AssertionTest
AssertionTest#prepare
gives you a hook to prepare the request that the test uses to execute.
This is the second step that runs in a test, and the last step before execute
.
The prepare
task is passed the results from setup
.
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- task function - a prepare task function - should accept a context containing the setup, and return a request object to be given to the executing task
assertionTest.execute(task) ⇒ AssertionTest
AssertionTest#execute
lets you specify the task that is executed in a test.
The execute
task is passed the results from prepare
.
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- task function - the task the test should execute, and capture results and errors from
assertionTest.verify(...tasks) ⇒ AssertionTest
AssertionTest#verify
lets you specify any number of tasks to verify the test results.
Each verify
task is passed a complete record of all test fixtures in an object,
including the setup, the request, the result, and the error (if an error was thrown)
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- ...tasks function - any number of verification tasks
assertionTest.teardown(task) ⇒ AssertionTest
AssertionTest#teardown
gives you a hook to tear down the test fixtures after execution.
The teardown
task is passed a complete record of all test fixtures in an object,
including the setup, the request, the result, and the error (if an error was thrown)
Kind: instance method of AssertionTest
Returns: AssertionTest - this
Params
- task function - a task to tear down the setup
assertionTest.build() ⇒ function
Builds the test case function.
Kind: instance method of AssertionTest
Returns: function - callback-expecting test function