Install Suites for NestJS, Inversify, Jest, Vitest, and Sinon
Step-by-step installation of Suites for NestJS or Inversify with Jest, Vitest, or Sinon. Includes tsconfig setup for emitDecoratorMetadata and experimentalDecorators.
Installation
Get Suites up and running in your project.
Prerequisites
- Node 18 or higher
- TypeScript project with decorators enabled
- A framework implementing IoC (NestJS, InversifyJS), or plain TypeScript classes (manually)
- A testing library (Jest, Vitest, or Sinon)
Installation
Install the core package:
npm install -D @suites/unitInstall the adapters for your framework and testing library:
# Install NestJS dependenciesnpm install @nestjs/common @nestjs/core reflect-metadata
# Install Suites core and adaptersnpm install --save-dev @suites/unit @suites/di.nestjs @suites/doubles.jest
# Install TypeScript and Jestnpm install --save-dev ts-jest @types/jest jest typescript# Install NestJS dependenciespnpm add @nestjs/common @nestjs/core reflect-metadata
# Install Suites core and adapterspnpm add -D @suites/unit @suites/di.nestjs @suites/doubles.jest
# Install TypeScript and Jestpnpm add -D ts-jest @types/jest jest typescript# Install NestJS dependenciesyarn add @nestjs/common @nestjs/core reflect-metadata
# Install Suites core and adaptersyarn add -D @suites/unit @suites/di.nestjs @suites/doubles.jest
# Install TypeScript and Jestyarn add -D ts-jest @types/jest jest typescriptThis installs three packages:
@suites/unit- Core TestBed API@suites/di.nestjs- NestJS adapter for reading DI metadata@suites/doubles.jest- Jest integration for creating type-safe mocks
Suites will automatically detect the installed adapters and configure itself accordingly.
Supported Libraries (Adapters)
Framework Adapters:
@suites/di.nestjs- NestJS dependency injection@suites/di.inversify- InversifyJS dependency injection
Available Mocking Libraries:
@suites/doubles.jest@suites/doubles.sinon@suites/doubles.vitest
After installation, no additional configuration for the test runner is needed.
Setting Up tsconfig.json
To use Suites, enable the emitDecoratorMetadata and experimentalDecorators options in the tsconfig.json file.
This configuration is necessary for Suites to reflect class dependencies and constructor metadata.
{ "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true }}Type Reference Configuration
To ensure type safety and maintain a clean API, Suites requires setting up type references for your mocking library. This approach enables importing all utilities from @suites/unit while maintaining proper type information.
Setting Up Type References
-
Create a
global.d.tsfile in the project root directory (or the location specified asrootDirin thetsconfig.json). -
Add reference types for the mocking library and dependency injection framework:
// Mocking library (choose one)/// <reference types='@suites/doubles.jest/unit' />/// <reference types='@suites/di.nestjs/types' />// OR/// <reference types='@suites/doubles.sinon/unit' />/// <reference types='@suites/di.inversify/types' />// OR/// <reference types='@suites/doubles.vitest/unit' />/// <reference types='@suites/di.nestjs/types' />Example for Jest + NestJS:
/// <reference types='@suites/doubles.jest/unit' />/// <reference types='@suites/di.nestjs/types' />This configuration ensures the project correctly recognizes the types provided by Suites (like the Mocked<T> type) without needing to import them from library-specific modules.
Monorepo Support
Suites is fully compatible with monorepo setups, accommodating projects that use different testing frameworks.
Recommended Setups
When using Suites in a monorepo, consider the following setups:
Using the same mocking and dependency injection framework across all workspaces
Install the corresponding adapter under the root directory of your monorepo.
Suites will automatically detect the adapter and configure itself accordingly.
Using different frameworks across workspaces
Install the corresponding adapter in each workspace separately. Configure the package manager’s hoisting settings
to enable Suites to detect the adapter in each workspace.
For Vitest Users
When integrating Suites with Vitest, additional configuration is required. Vitest typically uses esbuild for
TypeScript interpretation, which does not support emitDecoratorMetadata - a feature extensively used by Suites for
reflecting class dependencies. To overcome this, switch to using @swc/core via a
plugin, which supports emitDecoratorMetadata. For detailed guidance, see the Vitest documentation.
Here is an example of how to configure Suites with Vitest:
First, install the unplugin-swc and @swc/core packages and add it to the vitest.config.ts file as a plugin.
This will enable @swc/core to interpret TypeScript files and support emitDecoratorMetadata.
$ npm install --save-dev unplugin-swc @swc/coreimport swc from 'unplugin-swc';import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { globals: true, root: './' }, plugins: [ swc.vite({ module: { type: 'es6' }, jsc: { transform: { decoratorMetadata: true } }, }), ],});