2.0.8 • Published 2 months ago

@nestjs-toolkit/test-suite v2.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

Descrição

Suíte de testes para NestJS com Fastify.

Instalação

$ yarn add -D @nestjs-toolkit/test-suite

Setup

Criação de uma suíte de testes:

o MainSuite é a class que ira construir sua aplicação NestJs. Nesse exemplo nosso modulo principal é o CalcModule.

Obs: Tmb é possível criar uma suite de testes sem módulo, ou até mesmo fazendo override de um módulo existente.

Veja mais em criação de teste end-to-end.

import {Test, TestingModule} from '@nestjs/testing';
import {AbstractAppTestSuite} from '@nestjs-toolkit/test-suite';

export class MainSuite extends AbstractAppTestSuite {
    protected async createTestingModule(): Promise<TestingModule> {
        const module = Test.createTestingModule({
            imports: [AppModule],
        });

        return module.compile();
    }

    protected async beforeInitApp(): Promise<void> {
        // override
        await super.beforeInitApp();
    }

    async close(): Promise<void> {
        // override
        await super.close();
    }
}

Criação da classe pesona ira facilitar a criação de testes e2e:

import {ObjectId} from 'bson';
import jsonwebtoken from 'jsonwebtoken';
import {AbstractPersona} from '@nestjs-toolkit/test-suite/personas';
import {AbstractAppTestSuite} from '@nestjs-toolkit/test-suite';
import {MainSuite} from './main-suite';

export type FakeUser = {
    id: number,
    email: string,
    roles: string[]
};

export class UserPersona extends AbstractPersona<FakeUser> {
    public user: FakeUser = {
        id: 1,
        email: 'user@demo.com',
        roles: [],
    };

    protected async onInit(): Promise<void> {
        await super.onInit();
        // override
        // this.context.set('factory', new Foo());
    }

    protected isAuthorized(): boolean {
        return true;
    }

    protected getInstanceSuite(): AbstractAppTestSuite {
        // Important aqui é injetar a class MainSuite
        return new MainSuite();
    }

    protected generateJWT(): string {
        // Aqui ira montar o token que sera injetado no header das requisições
        return jsonwebtoken.sign({fake: 1, ...this.user}, 'secret');
    }
}

Como usar:

describe('CalcController (e2e)', () => {
    const userPersona = new UserPersona();

    beforeAll(async () => {
        await userPersona.init();
    });

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

    describe('/calc/sum (POST)', function () {
        it('Success', async () => {
            const response = await userPersona.http.request({
                url: '/calc/sum',
                method: 'POST',
                payload: {a: 1, b: 2},
            });

            response.dump().assertNoErrors().toBe('result', 3);
        });

        it('BadRequest', async () => {
            const response = await userPersona.http.request({
                url: '/calc/sum',
                method: 'POST',
                payload: {a: 1},
            });

            response
                .dump()
                .assertHasErrors()
                .assertStatusHttp(400)
                .assertErrorCode('CALC_VALIDATION')
                .assertMessage('a and b are required');
        });
    });
});

Teste

# unit tests
$ yarn test

# test coverage
$ yarn test:cov

Licença

MIT

2.0.8

2 months ago

2.0.7

3 months ago

2.0.6

3 months ago

2.0.3

9 months ago

2.0.5

8 months ago

2.0.4

8 months ago

2.0.2

12 months ago

2.0.1

12 months ago