hmil / ng-vacuum

Hassle-free unit tests for Angular
https://code.hmil.fr/ng-vacuum/
3 stars 1 forks source link
angular testing

NgVacuum

Angular tests in a vacuum.

Build Status Latest version

✅ Compatible with angular 7, 8, 9 and 10.

📖 Documentation


Overview

NgVacuum lets you write true unit tests for your Angular components and services in complete isolation and with minimal boilerplate.

This is what a typical test with NgVacuum looks like.

describe('AccessControlService', () => {
    let service: AccessControlService;

    beforeEach(() => {
        service = getService(AccessControlService);
    });

    it('redirects to logout when accessing the lobby while authenticated', () => {
        when(getMock(AuthService).isAuthenticated()).return(true);
        when(getMock(Router).navigate(['logout'])).resolve(true).once();
        expect(service.checkAccess('lobby')).toBe(false);
    });
});

The same test written with the stock angular tools is shown below.

describe('AccessControlService', () => {
    let service: AccessControlService;
    let authServiceMock: jasmine.SpyObj<AuthService>;
    let routerMock: jasmine.SpyObj<Router>;

    beforeEach(() => {
        authServiceMock = jasmine.createSpyObj<AuthService>('AuthService', [
            'isAuthenticated'
        ]);
        routerMock = jasmine.createSpyObj<Router>('Router', [
            'navigate'
        ]);
        TestBed.configureTestingModule({
            providers: [
                { provide: AuthService, useValue: authServiceMock },
                { provide: Router, useValue: routerMock},
                AccessControlService
            ]
        });
        service = TestBed.inject(AccessControlService);
    });

    it('redirects to logout when accessing the lobby while authenticated', () => {
        authServiceMock.isAuthenticated.and.returnValue(true);
        routerMock.navigate.and.returnValue(Promise.resolve(true));
        expect(service.checkAccess('lobby')).toBe(false);
        expect(routerMock.navigate).toHaveBeenCalledWith(['logout']);
    });
});

This example barely scratches the surface. NgVacuum packs a ton of useful features like shallow rendering and type-safe mocks which are designed to maximize the usefulness of unit tests while minimizing their maintainance cost.

Read the online documentation to get started.