Does NestJS Work With Vitest?

Fully CompatibleLast verified: 2026-02-20

Yes, NestJS and Vitest work together seamlessly for unit and integration testing with excellent performance and developer experience.

Quick Facts

Compatibility
full
Setup Difficulty
Easy
Official Integration
No — community maintained
Confidence
high
Minimum Versions
NestJS: 9.0.0
Vitest: 0.34.0

How NestJS Works With Vitest

NestJS and Vitest integrate naturally because both are modern TypeScript-first tools with zero configuration defaults. Vitest handles ESM and CommonJS modules that NestJS produces, and its Jest-compatible API means NestJS developers familiar with the default Jest setup transition instantly. The combination excels for testing NestJS modules, services, controllers, and decorators—Vitest's fast refresh and on-demand compilation significantly outpace Jest. You configure Vitest to use your tsconfig.json, and NestJS's dependency injection container works perfectly within Vitest's test scope. The main consideration is that NestJS testing utilities (Testing module) were built for Jest, but they're framework-agnostic and work without modification. Performance gains are substantial: typical test suites run 2-3x faster than Jest due to Vite's esbuild-powered transformation pipeline. Module mocking, spy utilities, and async lifecycle hooks all function identically to Jest.

Best Use Cases

Unit testing NestJS services and providers with rapid feedback loops during development
Integration testing controller endpoints with in-memory database providers and mocked external services
Testing custom decorators, guards, and middleware in isolation with Vitest's excellent mocking capabilities
Rapid CI/CD pipelines where test execution speed directly reduces deployment time

Quick Setup

bash
npm install -D vitest @vitest/ui ts-node
bash
// vitest.config.ts
import { defineConfig } from 'vitest/config';
import { compilerOptions } from './tsconfig.json';

export default defineConfig({
  test: {
    globals: true,
    environment: 'node',
    include: ['src/**/*.spec.ts'],
    testTimeout: 10000,
  },
  resolve: {
    alias: Object.fromEntries(
      Object.entries(compilerOptions.paths || {}).map(([key, [value]]) => [
        key.replace('/*', ''),
        value.replace('/*', ''),
      ])
    ),
  },
});

// src/app.service.spec.ts
import { describe, it, expect } from 'vitest';
import { AppService } from './app.service';

describe('AppService', () => {
  it('should return hello world', () => {
    const service = new AppService();
    expect(service.getHello()).toBe('Hello World!');
  });
});

Known Issues & Gotchas

warning

NestJS Testing module creates a full application context; Vitest tests may timeout on slow machines without proper configuration

Fix: Set testTimeout in vitest.config.ts to 10000+ ms. Use beforeAll/afterAll to create Testing module once per suite rather than per test.

warning

Module aliases in tsconfig.json (e.g., @app/*) require Vitest alias configuration to resolve correctly during tests

Fix: Mirror tsconfig paths in vitest.config.ts using the resolve.alias option or alias plugin.

info

Circular dependency detection differs between Jest and Vitest, occasionally exposing issues missed in Jest

Fix: Resolve circular imports in provider dependencies. This is actually beneficial—Vitest catches real architectural issues.

Alternatives

  • NestJS + Jest (official recommendation, slower but battle-tested)
  • NestJS + Mocha + Chai (lightweight alternative with manual setup)
  • Express + Vitest (lighter framework if NestJS overhead isn't needed)

Resources

Related Compatibility Guides

Explore more compatibility guides