- The file name should be Matching for the top level test suite name.
- There should be one top level test suite per file.
- Give a meaningful name or title for the test suite.
- Either use
var
orthis
to define variables that needs to use across tests.var
variables should be placed just below the top describe (test suite) - Use setup and teardown methods to perform common variable initializations and functionalities.
- A spec (test case) should test only “ONE” expectation.
- Group the main test suite with sub test suites to group sections.
Good Test Suite
Following is a structure of a good looking Jasmine test suite.
// GoodTestSpec.js
/**
* Describe the purpose of this test suite here.
*/
// give a meaningful name for the test suite.
// only "ONE" top level test suite per file.
describe('Good test suite', function () {
// define "ALL" the values that needs to be accessed across test cases
// Else use `this` keyword. But follow one approach. If `this` is used, don't define values here.
var foo = null,
bar = null,
numberOne = null;
// initialize "ALL" values above to their desired default value, in this block "beforeEach"
beforeEach(function () {
foo = null;
bar = null;
numberOne = 1;
// Using `this` approach. Pick one that suits the Test Suite.
this.moo = null;
});
// a spec (test case) should test only "ONE" expectation
it('should check foo to be null', function () {
// ....
expect(foo).toBe(null);
});
// another spec
it('should check numberOne to equal 1', function () {
// call fail() if something is not right and want to fail the test
// fail('woops! not correct state.');
expect(numberOne).toEqual(1);
});
// Group the main test suite with sub test suites to group sections.
// The title should be descriptive.
describe('that is magnificent', function () {
// Test name will read as "Good test suite that is magnificent should check 42 to is 42"
it('should check 42 is 42', function () {
expect(42).toEqual(42);
});
});
});