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

@nestjs-boilerplate/testing

v0.7.0

Published

NestJS Boilerplate is an open-source high-level framework for [NestJS](https://github.com/nestjs/nest) applications. It takes it up a notch by making it possible to solve repeatable tasks with less coding. It provides an architectural model based on Domai

Downloads

14

Readme

NestJS Boilerplate

NestJS Boilerplate is an open-source high-level framework for NestJS applications. It takes it up a notch by making it possible to solve repeatable tasks with less coding. It provides an architectural model based on Domain Driven Design and contains a variety of extra tools for a quick start developing NestJS applications. The NestJS Boilerplate aims to follow the principle of pluggable modules to extend functionality and less repeating common code from project to project. Inspired by Spring Framework, AspNet Boilerplate and Django.

Package description

Testing is a package that provides additional tools for testing NestJS Boilerplate applications.

Install

First

Install core package.

Then

$ npm install @nestjs-boilerplate/testing --save-dev

Test bootstrappers

NestJS Boilerplate defines a concept of using special util classes for bootstrapping your application or microservice. See more information here.

Testing package provides the following classes to bootstrap and starts an instance of Nest application or microservice in tests:

  • BaseTestBootstrapper class is the base class for other derived test bootstrappers. It extends BaseBootstrapper class and creates under the hood the instance of TestingModule from @nestjs/testing package which can be used in other derived test bootstrappers.
  • TestApplicationBootstrapper class configures and creates Nest application based on the instance of TestingModule. It extends BaseTestBootstrapper class.
  • TestMicroserviceBootstrapper class configures and creates Nest microservice based on the instance of TestingModule. It extends BaseTestBootstrapper class.
  • TestBootstrap class provides additional way to start Nest application or microservice. It uses TestApplicationBootstrapper class or TestMicroserviceBootstrapper class accordingly to perform bootstrapping.

Let's use test bootsrapper to bootstrap and start Nest application in tests.

import * as request from 'supertest';
import { TestBootstrap } from '@nestjs-boilerplate/testing';
import { AppModule } from './app.module';

describe('NoteController (e2e)', () => {
    let app;

    beforeAll(async () => {
        app = await new TestBootstrap(AppModule)
            .startApplication();
    });

    afterAll(async () => {
        await app.close();
    });

    describe('/api/notes (GET)', () => {
        it('should return success response', () => {
            return request(app.getHttpServer())
                .get('/api/notes')
                .expect(200);
        });
    });
});

Test mail

In some unit/e2e tests, we need to check that an email message is sent but at the same time, we need to make sure that we do not send any emails outside.

Testing package provides the following classes to mock sending email:

  • ConsoleMailService class allows redirecting emails to be print into the console.
  • MemoryMailService class allows keeping emails in special outbox parameter to have access to a list of sent emails.
  • NullMailService class that does nothing.

Let's override standard email service in tests.

import * as request from 'supertest';
import { TestingModule, TestingModuleBuilder } from '@nestjs/testing';
import { BaseMailService } from '@nestjs-boilerplate/core';
import { TestBootstrap, TestMailModule, MemoryMailService } from '@nestjs-boilerplate/testing';
import { AppModule } from './app.module';

describe('PostController (e2e)', () => {
    let mailService: MemoryMailService;
    let app;

    beforeAll(async () => {
        app = await new TestBootstrap(AppModule)
            .startApplication({
                testingMetadata: {
                    imports: [TestMailModule],
                },
                onCreateTestingModule: (builder: TestingModuleBuilder) => {
                    return builder
                        .overrideProvider(BaseMailService)
                        .useClass(MemoryMailService);
                },
                onTestingModuleCreated: (testingModule: TestingModule) => {
                    mailService = app.get<BaseMailService, MemoryMailService>(BaseMailService);
                },
            });
    });

    afterAll(async () => {
        await app.close();
    });

    describe('/api/posts/id/publish (POST)', () => {
        it('should publish post and send email', async () => {
            expect(mailService.outbox.length).toEqual(0);

            await request(app.getHttpServer())
                .post('/api/notes/1/publish')
                .expect(200);

            expect(mailService.outbox.length).toEqual(1);
        });
    });
});

class-validator constraints

If you want to use custom constraints for class validation but you need to write a unit test, then there may be a problem. class-validator library uses a dependency container by default to store/get the constraints. When you run the Nest application/microservice then NestJS Boilerplate automatically binds Nest container for class-validator. It might work for e2e tests but not for unit tests when you do not run Nest application/microservice.

Testing package contains createClassValidatorContainer function which allows to create and bind container for class-validator. Thus for unit tests, you can use this function to achieve the possibility to register constraints.

import { ClassValidator } from '@nestjs-boilerplate/core';
import { SimpleIocContainer, createClassValidatorContainer } from '@nestjs-boilerplate/testing';
import { UserVerificationService } from 'user-verification.service';
import { UsernameUniqueConstraint } from 'username-unique.constraint';
import { UserDto } from './user.dto';

describe('UserDto', () => {
    let container: SimpleIocContainer;

    beforeEach(async () => {
        const userVerificationService = new UserVerificationService();
        const usernameUniqueConstraint = new UsernameUniqueConstraint(userVerificationService);

        container = createClassValidatorContainer();
        container.register(UsernameUniqueConstraint, usernameUniqueConstraint);
    });

    describe('validate user dto', () => {
        it('should return success validation result', async () => {
            const dto = new UserDto('johnsmith', 'John', 'Smith');
            const result = await ClassValidator.valide(UserDto, dto);
            expect(result.isOk()).toBeTruthy();
        });
    });
});

Keep in touch

Dmitry Kologrivko - [email protected]

License

MIT LICENSE