Skip to main content

@nestjs-mod/testing

Modules and utilities for writing application tests

NPM version monthly downloads Telegram Discord

Installation

npm i --save-dev @nestjs/testing @nestjs-mod/testing

Modules

LinkCategoryDescription
DefaultTestNestApplicationCreatesystemDefault test NestJS application creator.
DefaultTestNestApplicationInitializersystemDefault test NestJS application initializer.

Modules descriptions

DefaultTestNestApplicationCreate

Default test NestJS application creator.

Use in NestJS-mod

Use without options.

import { bootstrapNestApplication } from '@nestjs-mod/common';
import { DefaultTestNestApplicationCreate } from '@nestjs-mod/testing';

bootstrapNestApplication({
modules: {
system: [DefaultTestNestApplicationCreate.forRoot()],
},
});

Example of use with override provider.

import { bootstrapNestApplication } from '@nestjs-mod/common';
import { DefaultTestNestApplicationCreate } from '@nestjs-mod/testing';
import { Injectable } from '@nestjs/common';

@Injectable()
export class CatsService {
findAll() {
return ['cats'];
}
}

const fakeCatsService = { findAll: () => ['test'] };

bootstrapNestApplication({
modules: {
system: [
DefaultTestNestApplicationCreate.forRoot({
staticConfiguration: {
wrapTestingModuleBuilder: (testingModuleBuilder) =>
testingModuleBuilder.overrideProvider(CatsService).useValue(fakeCatsService),
},
}),
],
},
});

Static configuration

KeyDescriptionConstraintsDefaultValue
wrapTestingModuleBuilderMethod for additional actions with TestingModuleBuilderoptional--
defaultLoggerDefault logger for applicationoptional--

Back to Top


DefaultTestNestApplicationInitializer

Default test NestJS application initializer.

Use in NestJS-mod

Use without options.

import { bootstrapNestApplication } from '@nestjs-mod/common';
import { DefaultTestNestApplicationCreate, DefaultTestNestApplicationInitializer } from '@nestjs-mod/testing';

bootstrapNestApplication({
modules: {
system: [DefaultTestNestApplicationCreate.forRoot(), DefaultTestNestApplicationInitializer.forRoot()],
},
});

An example of getting a provider after running a test application.

import { bootstrapNestApplication } from '@nestjs-mod/common';
import { DefaultTestNestApplicationCreate, DefaultTestNestApplicationInitializer } from '@nestjs-mod/testing';
import { Injectable } from '@nestjs/common';

@Injectable()
export class CatsService {
findAll() {
return ['cats'];
}
}

bootstrapNestApplication({
modules: {
system: [
DefaultTestNestApplicationCreate.forRoot(),
DefaultTestNestApplicationInitializer.forRoot({
staticConfiguration: {
postInit: async ({ app }) => {
if (app) {
const catsService = app.get(CatsService);
console.log(catsService.findAll());
}
},
},
}),
],
},
});

Static configuration

KeyDescriptionConstraintsDefaultValue
preInitMethod for additional actions before initoptional--
postInitMethod for additional actions after initoptional--
defaultLoggerDefault logger for test applicationoptional--

Back to Top

License

MIT