npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smazzoleni/ng-bench

v13.0.0

Published

NgBench is a library that enhances the original `@angular/core/testing/TestBed`. Its goal is to reduce boiler plate when writing unit tests for angular project using jests.

Downloads

4

Readme

NgBench

NgBench is a library that enhances the original @angular/core/testing/TestBed. Its goal is to reduce boiler plate when writing unit tests for angular project using jests.

NgBench relies on two external libraries:

  • https://testing-library.com/docs/angular-testing-library/intro
  • https://www.npmjs.com/package/ng-mocks

NgBench takes advantage of Typescrit's generics and type definitions to enhance test writing experience, by offering a fluent syntax and rich accurate typing.

Disclaimer

This library is still in it's early days and it's API is a work in progress. There might be major breaking changes in the next weeks, so use it at your own risks. The version number is aligned on angular version.

Prerequisites

  • @angular/common: >=11.0.0
  • @angular/core: >=11.0.0
  • @testing-library/angular: >=10.0.0
  • @testing-library/dom: >=8.10.0
  • @testing-library/jest-dom: >=5.11.0
  • @testing-library/user-event: >=12.0.0
  • jest: >=26.0.0
  • lodash: >=4.17.15
  • ng-mocks: >=10.0.0
  • typescript: >=3.8.0

We assume you are using jest for unit testing your Angular applications. If you're not, you may take a look at Jest builder for Angular build facade to replace easily karma/jasmine by jest.

Although not required, we recommend usage of these extra jest matchers:

Installation

npm install -D @smazzoleni/ng-bench @testing-library/angular lodash ng-mocks

or

yarn add -D @smazzoleni/ng-bench @testing-library/angular lodash ng-mocks

Usage

The default app.component.spec.ts generated by Angular CLI can now be replaced by

import { RouterTestingModule } from '@angular/router/testing';
import { Host, NgBench } from '@smazzoleni/ng-bench';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
    let host: Host<AppComponent>;

    beforeEach(async () => {
        host = await NgBench.testComponent(AppComponent)
            .import(RouterTestingModule)
            .createHost();
    });

    it('should create the app', () => {
        expect(host.testInstance).toBeTruthy();
    });

    it(`should have as title 'ng-bench'`, () => {
        expect(host.testInstance.title).toEqual('ng-bench');
    });

    it('should render title', () => {
        expect(
            host.container.querySelector('.content span').textContent,
        ).toContain('ng-bench app is running!');
    });
});

However, according to @testing-library guidelines, the tests should resemble the way the software is used and deal with DOM nodes rather than component instances. Also, we don't want the tests to be tied to the document internal structure. Hence, tests should look more like:

import { RouterTestingModule } from '@angular/router/testing';
import { Host, NgBench } from '@smazzoleni/ng-bench';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
    let host: Host<AppComponent>;

    beforeEach(async () => {
        host = await NgBench.testComponent(AppComponent)
            .import(RouterTestingModule)
            .createHost();
    });

    it('should create', () => {
        expect(host.container).toBeInTheDocument();
    });

    it(`should have as title 'ng-bench'`, () => {
        expect(
            host.queryByText('ng-bench app is running!'),
        ).toBeInTheDocument();
    });
});

and now what?

Example above just scratches the surface of the possibilities. More detailed examples are available here