This module provides unit testing functionality that integrates with the framework. It is a declarative framework, using decorators to define tests and suites. The test produces results in the TAP 13
format.
The test-plugin
directly integrates with the module to provide real-time feedback on unit tests.
A test suite is a collection of individual tests. All test suites are classes with the @Suite
decorator. Tests are defined as methods on the suite class, using the @Test
decorator. All tests intrinsically support async/await.
Additionally, the the suite classes support Dependency Injection
.
A simple example would be:
import * as assert from 'assert';
@Suite()
class SimpleTest {
private complexService: ComplexService;
@Test()
async test1() {
let val = await this.complexService.doLongOp();
assert(val === 5);
}
@Test()
test2() {
assert(/abc/.test(text));
}
}
A common aspect of the tests themselves are the assertions that are made. Node
provides a built-in assert
library. The framework uses AST transformations to modify the assertions to provide integration with the test module, and to provide a much higher level of detail in the failed assertions.
For example:
assert({size: 20, address: { state: 'VA' }} === {});
would generate the error:
AssertionError(
message="{size: 20, address: {state: 'VA' }} should deeply strictly equal {}"
)
travetto-test
is packaged as a included script to execute tests from the command line. The script can be invoked as
./node_modules/.bin/travetto-test test/.*
All tests should be under the test/.*
folders. The pattern for tests is defined as a regex and not standard globbing.