ng-vacuum
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

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 barly 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.

Readme

Keywords

none

Package Sidebar

Install

npm i ng-vacuum

Weekly Downloads

897

Version

0.3.1

License

MIT

Unpacked Size

256 kB

Total Files

32

Last publish

Collaborators

  • hmil