Reputation: 4516
In Angular I want to share some utils for using across my tests.
What would be the best practice/standard to do so?
I.e. To test a component/service/whatever you create a file with the same name and the extension .spec.ts
and placed in the same folder. For a test utils what will be the best placement and naming? Also ending in .spec.ts
?
Upvotes: 1
Views: 2583
Reputation: 785
Util functions can be tested without any TestBed. There is no need to initialize anything either. Consider that util functions should be pure functions (stateless). Creating different blocks for the different functions is good. Other generic best practices can be found here: https://github.com/CareMessagePlatform/jasmine-styleguide
// test cases for isEmpty function
describe('CommonUtil', function () {
describe('.isEmpty(x)', function () {
it('should returns true when x is null', function () {
expect(CommonUtil.isEmpty(null)).toBeTrue();
});
it('should returns false when x is an object', function () {
expect(CommonUtil.isEmpty({ dummy: 123 })).toBeFalse();
});
});
});
Upvotes: 3
Reputation: 26150
The file extension .spec.ts
is used so that Karma can identify the files that contain tests. This is what you specify inside the test.ts
file.
const context = require.context('./', true, /\.spec\.ts$/);
I would place test utils classes in src/app/shared/testing
folder within a .ts
file each. To improve the reliability of your tests (and to improve overall code coverage statistics), you should also have a corresponding .spec.ts
file for each test utils class.
Upvotes: 1