# @nestjs-toolkit/test-suite

> Test suite for NestJS applications

Latest version **2.0.13** (published 2024-12-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nestjs-toolkit/test-suite
pnpm add @nestjs-toolkit/test-suite
yarn add @nestjs-toolkit/test-suite
bun add @nestjs-toolkit/test-suite
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.13 |
| Published | 2024-12-04 |
| First published | 2023-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 63.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Valmir Barbosa |
| Maintainers | valmir.php |

## Links

- npm: https://www.npmjs.com/package/@nestjs-toolkit/test-suite
- npm.io page: https://npm.io/package/@nestjs-toolkit/test-suite

## Dependencies (3)

- [bson](https://npm.io/package/bson.md) ^6.9.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [form-data](https://npm.io/package/form-data.md) ^4.0.1

## Recent versions

- 2.0.13 (latest) — 2024-12-04
- 2.0.12 — 2024-12-03
- 2.0.11 — 2024-10-31
- 2.0.10 — 2024-10-26
- 2.0.9 — 2024-06-20
- 2.0.8 — 2024-03-21
- 2.0.7 — 2024-01-30
- 2.0.6 — 2024-01-30
- 2.0.5 — 2023-09-20
- 2.0.4 — 2023-09-04
- 2.0.3 — 2023-08-12
- 2.0.2 — 2023-05-23
- 2.0.1 — 2023-05-22

## README

## Descrição

Suíte de testes para NestJS com Fastify.

## Instalação

```bash
$ 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](https://docs.nestjs.com/fundamentals/testing#end-to-end-testing).

```typescript
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:

```typescript
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:

```typescript
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

```bash
# unit tests
$ yarn test

# test coverage
$ yarn test:cov
```

## Licença

[MIT](LICENSE)

---
_Source: https://npm.io/package/@nestjs-toolkit/test-suite · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
