-
Notifications
You must be signed in to change notification settings - Fork 3
How to unit test
sholzer edited this page May 15, 2016
·
1 revision
Basic File
/**
* Test template
*/
describe("SomeClass specification", ()=>{
var some: Set = up;
describe("A test group", ()=>{
var testGroup: Dependant = setUp;
it("A single Unit test", ()=>{
expect(aThing).toBe(anotherThing);
});
});
});
You can group tests and their set ups within a describe(). The string passed as first parameter works as the groups title and will be displayed in the test execution. The unit tests and the test set ups are passed as normal typescript code within the curly brackets in the ()=>{...} parameter.
The Unit tests are described in the it() method. You can use the expect().toBe() method similiar to the assert methods of JUnit. I also wrote some custom methods that may fit our needs more:
assert Methoden für Tests
/**
* Tests if the JSON representation of two objects is equal (we don't need the exact reference but only an equal)
* @author sholzer 160511 (I wanted an Junit equivalent of assertEquals())
* @param input :any an object
* @param expectation :any the object input is expected to be equal
* @return void. Calls fail() if JSON.stringify(input) != JSON.stringify(expectation)
*/
function assertEqualJson(input: any, expectation: any): void {
if (JSON.stringify(input) !== JSON.stringify(expectation)) {
fail('Expected\n' + JSON.stringify(input) + '\nto be equal to\n' + JSON.stringify(expectation));
}
}
/**
* Tests if the JSON representation of two objects is NOT equal (we don't need the exact reference but only an equal)
* @author sholzer 160511 (I wanted an Junit equivalent of assertEquals())
* @param input :any an object
* @param expectation :any the object input is expected NOT to be equal
* @return void. Calls fail() if JSON.stringify(input) == JSON.stringify(expectation)
*/
function assertNotEqualJson(input: any, expectation: any): void {
if (JSON.stringify(input) === JSON.stringify(expectation)) {
fail('Expected\n' + JSON.stringify(input) + '\nNOT to be equal to\n' + JSON.stringify(expectation));
}
}