@wisemen/nestjs-tests
v3.0.0
Published
Testing helpers for NestJS API packages, with custom `expect` matchers and a stubbed TypeORM `DataSource` for transaction-based unit tests.
Maintainers
Keywords
Readme
@wisemen/nestjs-tests
Testing helpers for NestJS API packages, with custom expect matchers and a
stubbed TypeORM DataSource for transaction-based unit tests.
Installation
pnpm add -D @wisemen/nestjs-testsPeer dependencies
pnpm add -D @wisemen/nestjs-domain-events typeormUsage
Register the matchers once
import { expect } from 'expect'
import {
ISO8601,
isEnumValue,
toHaveEmitted,
toHaveErrorCode,
toHaveStatus,
toHaveValidationErrors,
uuid,
} from '@wisemen/nestjs-tests'
expect.extend({
ISO8601,
isEnumValue,
toHaveEmitted,
toHaveErrorCode,
toHaveStatus,
toHaveValidationErrors,
uuid,
})Assert HTTP responses and DTO validation
import { HttpStatus } from '@nestjs/common'
expect(response).toHaveStatus(HttpStatus.CREATED)
expect(response).toHaveErrorCode('validation_error')
expect(response.body).toStrictEqual(expect.objectContaining({
uuid: expect.uuid(),
createdAt: expect.ISO8601(),
}))
await expect(command).toHaveValidationErrors()
expect(role).isEnumValue(UserRole)Stub transactional use cases
stubDataSource() creates a Sinon stub for DataSource and executes
transaction(...) callbacks with a stubbed EntityManager.
import { createStubInstance } from 'sinon'
import { DomainEventEmitter } from '@wisemen/nestjs-domain-events'
import { stubDataSource } from '@wisemen/nestjs-tests'
const eventEmitter = createStubInstance(DomainEventEmitter)
const useCase = new CreateOrderUseCase(
stubDataSource(),
createStubInstance(OrderRepository),
eventEmitter,
)
const response = await useCase.execute(command)
expect(eventEmitter).toHaveEmitted(new OrderCreatedEvent(response.uuid))License
GPL
Parallel test runner
The package includes a CLI for running compiled Node test files in a fixed number of long-lived processes. Each process runs its assigned files with Node test isolation disabled, allowing a test bench to reuse setup within that process while keeping process state separate from other workers.
pnpm exec nestjs-tests-parallel \
--env-file=.env.test \
--workers=2 \
--test="dist/src/**/*.test.js" \
--global-setup=dist/src/modules/test-setup/global-setup.js--workers defaults to 2. --test defaults to dist/src/**/*.test.js, so
the glob includes both the build directory and test file pattern. --global-setup
defaults to the path shown above. The global setup module must export an async or
sync globalSetup function. --env-file is optional; when provided, the CLI
loads it before global setup and passes the resulting environment to each test
process. The runner sets generic TEST_RUN_ID and TEST_WORKER_IDS environment
variables for global setup, then assigns each child process its one-based
TEST_WORKER_ID. Use getTestRunId(), getTestWorkerIds(), and
getTestWorkerId() from this package to read them. These helpers return
undefined when a value is not configured and reject malformed IDs. A global
setup can use the run ID and worker IDs to provision per-worker resources. If
the setup module exports
globalTeardown, the runner calls it after all workers finish, including when
global setup fails.
