Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ngx-testing-tools

remscodes389MIT3.0.2TypeScript support: included

Makes Angular testing easier

angular, test, testing, tools, mock, component, directive, service, interceptor, guard, router, testbed, pipe, provider, jasmine, fixture, ngx

readme

Angular Testing Tools

Makes Angular testing easier

github ci codecov coverage npm version bundle size license

In a nutshell

This library aims to reduce boilerplate 😎 and provides high-level tools️ 🔥 for testing Component, Guard, Interceptor and everything else related to the Angular mechanism.

It makes tests easier to read 😌 and faster to write ⚡️!

Quick examples

Testing Component

describe('AppComponent', () => {
  const tb = componentTestBed(AppComponent) // 🛠️ Create the test bed which is re-compiled for each test
    .inject('prefs', Preferences); // 🖇️ Link a key to an injection for all tests, see below 👇

  it('should render title', tb(({ component, query }) => { // 🔋 Access enhanced tools for testing components 
    expect(component.title).toEqual('app-v17');
    const span = query.findElement('.content span');
    expect(span.textContent).toContain('app-v17 app is running!');
  }));

  it('should update preferences on click', tb(({ action, injected: { prefs } }) => { // 🤯 Retrieve injections by autocompletion
    expect(prefs.approved).toBeFalse();
    action.click('#my-button');
    expect(prefs.approved).toBeTrue();
  }));
});

🫡 (The redundant "should create" test is even called up for you!)

Testing Service

describe('AppService', () => {
  const tb = serviceTestBed(AppService, { httpTesting: true }); // 🛠️ Create the test bed and enable http testing

  it('should fetch cat fact', tb(({ service, http, rx }, done) => {
    const mockRes = { fact: 'string', length: 6 };

    rx.remind = service.getCatFact().subscribe({ // 🧯 Use rx.remind to auto unsubscribe after the end of the test
      next: (res) => {
        expect(res).toEqual(mockRes);
        done();
      },
    });

    http.emitSuccessResponse({ url: service.CAT_FACT_URL, body: mockRes }); // 🎭 Fake the http response of the request that matches the url
  }));
});

Installation

npm install --save-dev ngx-testing-tools

Documentation

Visit the docs at https://remscodes.github.io/ngx-testing-tools.

Demo

Check demo .spec.ts files.

Version compatibility

Compatible with Angular >= 15.2.x.

What's next ? 🤩

  • Mocks
  • Angular schematics

License

MIT © Rémy Abitbol.

changelog

2.3.0 (2024-02-08)

Features

  • PipeTestBed.
  • InterceptorTestBed.

Improvements

  • Codebase.
  • Project tree.
  • Tests.
  • Docs.

Deprecations

Will be removed in v3.

  • testPipeValues (use PipeTestBed instead).
  • makeInterceptorSucceed (use InterceptorTestBed instead).
  • makeInterceptorFail (use InterceptorTestBed instead).

Commits : https://github.com/remscodes/ngx-testing-tools/commits/v2.3.0

2.2.0 (2024-01-31)

Features

  • ServiceTestBed.
  • ModuleTestBed.
  • HttpTestingTools.
  • Many custom test beds options.

Improvements

  • Codebase.
  • Tests.
  • Docs.

Deprecations

Will be removed in v3.

  • tb.compileEach() (use test bed option autoCompile instead (true by default)).
  • tb.shouldCreate() (use test bed option checkCreate instead (true by default)).
  • findComponent(fixture, ..) (use ComponentTools.query instead).
  • findAllComponents(fixture, ..) (use ComponentTools.query instead).
  • findElement(fixture, ..) (use ComponentTools.query instead).
  • findAllElements(fixture, ..) (use ComponentTools.query instead).
  • findDebugElement(fixture, ..) (use ComponentTools.query instead).
  • findAllDebugElements(fixture, ..) (use ComponentTools.query instead).
  • click(fixture, ..) (use ComponentTools.action instead).
  • emitOutput(fixture, ..) (use ComponentTools.action instead).
  • emitFakeSuccessResponse(httpController, ..) (use HttpTools instead).
  • emitFakeErrorResponse(httpController, ..) (use HttpTools instead).
  • expectHttpRequest(httpController, ..) (use HttpTools instead).
  • fromInjector(..) (use BaseTools.injector instead).
  • exportModuleToCreate(..) (use ModuleTestBed instead).

Commits : https://github.com/remscodes/ngx-testing-tools/commits/v2.2.0

2.1.0 (2024-01-19)

Features

  • tb.compileEach().
  • tb.setup(..).
  • tb.inject(..).
  • Update Angular 17 compatibility (17.x instead of 17.0.x).

Improvements

  • Docs.
  • Tests.

Deprecation

Will be removed in v3.

  • ComponentTools.debug.

Commits : https://github.com/remscodes/ngx-testing-tools/commits/v2.1.0

2.0.1 (2024-01-14)

Fixes

  • Remove DestroyRef from ComponentTools.

Commits : https://github.com/remscodes/ngx-testing-tools/commits/v2.0.1

2.0.0 (2024-01-14)

Features

  • ComponentTestBed.

Improvements

  • Documentation.
  • Add unit and integrations tests.
  • Add code coverage.
  • Add demo.
  • Rename package to ngx-testing-tools.

Commits : https://github.com/remscodes/ngx-testing-tools/commits/v2.0.0

1.0.0 (2024-01-02)

Features

  • Component testing : instance, native element, debug element and event.
  • Pipe testing.
  • HTTP testing : response and interceptor.
  • Router testing : guard and resolver.
  • Injector testing.
  • Module testing.

Commits : https://github.com/remscodes/ngx-testing-extra/commits/v1.0.0