Describe provides a simple method for testing asynchronous and synchronous code within JavaScript projects.
describe( groupName, tests[, options] );
- groupName (string): A human-readable description of the test group.
- tests (object): An object made up of human-readable test descriptions as
keys and functions to run as tests. Tests functions will be given access to
this.expect
. - options: Configuration options. Possible values:
- timeout (int): The max amount of time in milliseconds to wait for a test to run before timing out.
- callbackMode (string): if set to 'node', this.expect will treat the first argument to the callback as an error and the second argument as the result.
describe.config( key, value )
Sets the global configuration for tests.
this.expect( subject, expected )
- subject (mixed): the actual result.
- expected (mixed): the expected result.
describe("assertions", {
'basic synchronous expectation': function() {
this.expect(42, 42);
}
});
this.expect( expected )
function addNumbersAsync(a, b, callback) {
callback(a+b);
}
describe("assertions", {
'basic asynchronous expectation': function() {
addNumbersAsync(2, 2, this.expect(4));
}
});
function addNumbersAsync(a, b, callback) {
callback(null, a+b);
}
describe("assertions", {
'basic asynchronous expectation': function() {
addNumbersAsync(2, 2, this.expect(4));
}
}, { callbackMode: 'node' });
An asynchronous method. Calls back with the results of all tests described up to that point. You should probably wait until you're done defining tests to call this.
{
passed: 1,
total: 2,
results: {
'sample test group': {
passed: 1,
total: 2,
results: {
'this test passed because its error is null': null,
'this test failed because there's an error': [Error]
}
}
}
}
Gets the results and outputs them either to the DOM or the console.