Skip to main content

@hodfords/nestjs-testing

Nest Logo

Nestjs-Testing is a testing library for Nestjs applications. It provides a set of utilities to help you test your Nestjs application.

Requirements 📋​

@hodfords/nestjs-testingNestJSNodeTest runner
12.x12.x>=20.19Vitest >=4
11.x11.x>=18Jest

This package is ESM-only. It ships "type": "module" and can only be loaded with import (require() is not supported). Node >=20.19 (or >=22.12 / >=24.15 / >=26) is required.

Installation 🤖​

To begin using it, we first install the required dependencies.

npm install @hodfords/nestjs-testing

@nestjs/common, @nestjs/core, @nestjs/testing and vitest are peer dependencies and must be installed in your project.

Relative imports inside your own test files must carry the .js extension, as required by moduleResolution: nodenext:

import { TestHelper } from './test.helper.js';

Configuration 🚀​

To easily customize the configuration, let's create an object that extends the BaseTestHelper class. This object will be used to configure the test environment.

export class TestHelper extends BaseTestHelper {
getSupertestConfig(): SupertestConfig {
return {
isUseBearerAuth: true,
authenticationHeader: 'Authorization',
workspaceHeader: 'x-workspace-id'
};
}

getTestModuleBuilder(): TestingModuleBuilder {
return Test.createTestingModule({
imports: [AppModule]
});
}
}

Usage 🚀​

Write your test cases using the TestHelper class.

import { afterAll, beforeAll, describe, it } from 'vitest';
import { TestHelper } from './test.helper.js';

describe('AppController (e2e)', () => {
const testHelper = new TestHelper();

beforeAll(async () => {
await testHelper.initialize();
});

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

it('Get index success', async () => {
return testHelper.get('/').isOk().expect('Hello World!');
});
});

BaseTestHelper.close() calls Vitest's vi.restoreAllMocks() (it used to call jest.restoreAllMocks()), so the helper must be used from inside a Vitest run.

License​

This project is licensed under the MIT License