nestjs-mocha-decorators
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Nest.js Mocha Decorators

This library lets you use test decorators to create your test suite, with all these benefits

  • You can create tests suites and tests using classes, instead of jest or mocha anonymous functions
  • You can inject your components in your tests, instead of using app.get(Component to use)
  • You can use inheritance
  • You can pick which test or suite you want to run

Example

```
@TestSuite('Demo Test Suite')
export class DemoTest {

    @Inject() 
    private readonly httpService: HttpService

    @Test('Get Test')
    async testGet(): Promise<void> {
        const { data } = await firstValueFrom(this.httpService.get('http://localhost:4343/demo'))
        expect(data).toBe('Hello, Test world !')
    }
}
```

Installation

Install with npm globally: ( as a development dependency because it will be use for tests only )

npm i --save-dev nestjs-mocha-decorators

Configuration

Its better with an example.

  • Add your tests entry point in test directory ( test/test.entrypoint.ts )
import { HttpModule } from '@nestjs/axios';
import { ConsoleLogger, INestApplication } from '@nestjs/common';
import { Test, TestingModule, TestingModuleBuilder } from '@nestjs/testing';
import { MochaTestModule, MochaTestService } from '../src'; // Change this to import { MochaTestModule, MochaTestService } from 'nestjs-mocha-decorators'
import { DemoTest } from './tests/demo.test';
import { YourAppModule } from './your-app/your-app.module';
import { run } from 'mocha';

const initNest = async (): Promise<INestApplication> => {
  const testingModuleBuilder: TestingModuleBuilder = await Test.createTestingModule({
    imports: [
      YourAppModule,  // The app to test
      MochaTestModule.registerTests([DemoTest], [HttpModule]) // Your test suite
    ],
  })
  const testingModule: TestingModule = await testingModuleBuilder.compile();
  const app = testingModule.createNestApplication();
  app.useLogger(new ConsoleLogger());
  app.listen(4343)
  return app
}

(async (): Promise<void> => {
  const app = await initNest();
  app.get(MochaTestService).declareTests()
  run();
})();
  • Add this line to your package.json
    "test": "mocha --delay --exit --require ts-node/register --require tsconfig-paths/register test/test.entrypoint.ts"
  • Start coding your tests
import { HttpService } from "@nestjs/axios";
import { Inject } from "@nestjs/common";
import { firstValueFrom } from "rxjs";
import { Test, TestSuite } from "../../src";
import expect from 'expect'


@TestSuite('Demo Test Suite')
export class DemoTest {

    @Inject()
    private readonly httpService: HttpService

    @Test('Get Test')
    async testGet(): Promise<void> {
        const { data } = await firstValueFrom(this.httpService.get('http://localhost:4343/demo'))
        expect(data).toBe('Hello, Test world !')
    }
}
  • Run all your tests with:
npm run test
  • If you want to run the tests of a single class ( suite ), you can:
SUITETORUN="Demo Test Suite" npm run test
  • Or, perhaps you want to run a single test, with:
TESTTORUN="Get Test" npm run test

Comments, suggestions and more

Creating a ticket on github can be very annoying ... send me your comments, problems and suggestions to mcarrizo@gmail.com

Best !

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.5142latest
1.0.00alpha

Version History

VersionDownloads (Last 7 Days)Published
1.0.5142
1.0.80
1.0.4-alpha1
1.0.30
1.0.20
1.0.00
0.0.271
0.0.260
0.0.230
0.0.220
0.0.210
0.0.200
0.0.190
0.0.180
0.0.170
0.0.160
0.0.150
0.0.140
0.0.131
0.0.120
0.0.110
0.0.101
0.0.91
0.0.80
0.0.71
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i nestjs-mocha-decorators

Weekly Downloads

148

Version

1.0.5

License

UNLICENSED

Unpacked Size

113 kB

Total Files

15

Last publish

Collaborators

  • demostenes1509