You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A set of utilities to make testing with NexusDI easier, including test containers, provider overrides/mocks, and helpers for setup/teardown. The goal is to streamline unit and integration testing for DI-based applications, regardless of the test runner.
Motivation
Simplify DI container setup in tests: Reduce boilerplate for creating and configuring containers in test environments.
Easy mocking/overriding of providers: Allow tests to replace real services with mocks, stubs, or spies.
Helpers for lifecycle management: Make it easy to manage container setup/teardown and resource cleanup.
Test runner agnostic: Work with Jest, Vitest, Mocha, etc.
Proposed Features
createTestContainer() utility for fast test container setup
Provider overrides/mocks for easy dependency replacement
Helpers for setup/teardown (e.g., beforeEach, afterEach)
Utilities for asserting DI graph, provider resolution, and instance lifetimes
Support for async setup/teardown and resource disposal
Optionally, built-in mocks for common modules (e.g., cache, logger)
Example Workflow
Create a test container with overrides and get the SUT:
test('should fetch user info when processing order',async()=>{awaitorderService.processOrder({orderId: 123,userId: 1});expect(mockUserService.getUser).toHaveBeenCalledWith(1);// ...other assertions on orderService behavior...});
Assert provider resolution and DI graph:
import{assertProviderResolved}from'@nexusdi/testing';test('should resolve OrderService and its dependencies',()=>{assertProviderResolved(container,OrderService);assertProviderResolved(container,UserService);});
Open Questions
What test runners should be supported out of the box (Jest, Vitest, Mocha, etc.)?
Should we provide built-in mocks for common modules (cache, logger, etc.)?
How to best support async setup/teardown and resource disposal?
Should we provide utilities for snapshotting or visualizing the DI graph?
Any other helpers/utilities that would make testing easier?
API Sketch
import{createTestContainer}from'@nexusdi/testing';import{UserService}from'../src/services/UserService';import{OrderService}from'../src/services/OrderService';constmockUserService={getUser: jest.fn().mockResolvedValue({id: 1,name: 'Test User'}),};letcontainer: ReturnType<typeofcreateTestContainer>;letorderService: OrderService;beforeEach(()=>{container=createTestContainer({overrides: [{provide: UserService,useValue: mockUserService},],});orderService=container.get(OrderService);// SUT});afterEach(()=>container.dispose());test('should fetch user info when processing order',async()=>{awaitorderService.processOrder({orderId: 123,userId: 1});expect(mockUserService.getUser).toHaveBeenCalledWith(1);});
Call for Feedback
What testing patterns do you use with DI?
What helpers/utilities would make your tests easier?
Any pain points with testing DI-based code?
Should we provide built-in mocks for common modules?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
A set of utilities to make testing with NexusDI easier, including test containers, provider overrides/mocks, and helpers for setup/teardown. The goal is to streamline unit and integration testing for DI-based applications, regardless of the test runner.
Motivation
Proposed Features
createTestContainer()utility for fast test container setupbeforeEach,afterEach)Example Workflow
Create a test container with overrides and get the SUT:
Use the SUT in your tests:
Assert provider resolution and DI graph:
Open Questions
API Sketch
Call for Feedback
Beta Was this translation helpful? Give feedback.
All reactions