๐ Technical Documentation: Testing - Unit Tests
๐ฏ Overview
Unit testing is a critical part of the development process in this Angular-based project. It ensures the reliability and correctness of individual components and services. This project uses Jasmine as the testing framework, and test files are written with the .spec.ts extension. These tests verify the behavior of components, services, and other functionalities.
๐ ๏ธ Unit Testing Framework
- Framework: Jasmine
- Test Runner: Karma
- File Convention: Unit test files are named with the
.spec.tssuffix.
๐งช Test Locations and Examples
Below is a table summarizing some of the unit tests included in the project:
| Filename | Description |
|---|---|
brand-profile.service.spec.ts |
Tests the creation and functionality of the BrandProfileService. |
brands.component.spec.ts |
Ensures the BrandsComponent is created and rendered correctly. |
orders.service.spec.ts |
Validates the OrdersService for interacting with order-related APIs. |
helper.service.spec.ts |
Tests the HelperService for its utility functions. |
balances.component.spec.ts |
Verifies the BalancesComponent is functioning and rendering as expected. |
requested-retailer.component.spec.ts |
Confirms the RequestedRetailerComponent is created successfully. |
๐ Unit Test Example
Service Test Example: BrandProfileService
This test ensures that the BrandProfileService is correctly instantiated.
import { TestBed } from '@angular/core/testing';
import { BrandProfileService } from './brand-profile.service';
describe('BrandProfileService', () => {
let service: BrandProfileService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(BrandProfileService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
- Purpose: Verifies that the service is successfully created.
- Assertion:
expect(service).toBeTruthy();checks if the service instance is valid.
Component Test Example: BrandsComponent
This test ensures that the BrandsComponent is created and initialized properly.
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BrandsComponent } from './brands.component';
describe('BrandsComponent', () => {
let component: BrandsComponent;
let fixture: ComponentFixture<BrandsComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BrandsComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BrandsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
- Purpose: Ensures the component is instantiated and rendered without errors.
- Assertion:
expect(component).toBeTruthy();confirms the component's existence.
๐ How to Run Unit Tests
-
Install dependencies:
npm install -
Run the tests:
npm testThis will execute the test suite using Karma as the test runner.
-
View results:
- Test results will appear in the terminal.
- A browser-based Jasmine Spec Runner may also display the results.
๐ Best Practices for Unit Testing
- Isolate Tests: Each test should focus on a single functionality.
- Mock Dependencies: Use Angular's dependency injection to mock services and test components in isolation.
- Write Descriptive Test Names: Clearly state the purpose of each test.
- Code Coverage: Aim for high test coverage to ensure all code paths are tested.
๐ Configuration Files
Here are some important configuration files related to testing:
| File | Purpose |
|---|---|
karma.conf.js |
Configures the Karma test runner, including browsers and reporters. |
tsconfig.spec.json |
TypeScript configuration for test files. |
Example: karma.conf.js
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage/volley-app'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
๐ Conclusion
Unit testing in this Angular project ensures the stability and reliability of the application. By leveraging Jasmine and Karma, the project maintains a robust testing framework for both components and services. Following best practices and running tests regularly will help detect issues early and improve overall code quality. ๐