Writing Tests
This guide covers patterns and conventions for writing tests in the Ever Works platform, with examples from the actual codebase.
Testing NestJS Services
Most agent package services are tested using NestJS's Test.createTestingModule. The pattern is:
- Create mock implementations of dependencies.
- Build a testing module with the service under test and mock providers.
- Get the service instance from the compiled module.
- Assert behavior.
Example: Testing PluginRegistryService
import { Test, TestingModule } from '@nestjs/testing';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { PluginRegistryService } from '../services/plugin-registry.service';
import { WorkPluginRepository } from '../repositories/work-plugin.repository';
import { UserPluginRepository } from '../repositories/user-plugin.repository';
describe('PluginRegistryService', () => {
let service: PluginRegistryService;
let eventEmitter: EventEmitter2;
let workPluginRepository: jest.Mocked<WorkPluginRepository>;
beforeEach(async () => {
workPluginRepository = {
findByWorkAndPlugin: jest.fn()
} as unknown as jest.Mocked<WorkPluginRepository>;
const module: TestingModule = await Test.createTestingModule({
providers: [
PluginRegistryService,
{
provide: EventEmitter2,
useValue: { emit: jest.fn(), on: jest.fn(), off: jest.fn() }
},
{
provide: WorkPluginRepository,
useValue: workPluginRepository
},
{
provide: UserPluginRepository,
useValue: { findByUserAndPlugin: jest.fn() }
}
]
}).compile();
service = module.get<PluginRegistryService>(PluginRegistryService);
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
});
afterEach(() => {
service.clear();
});
it('should register a plugin', () => {
const plugin = createMockPlugin('test-plugin');
service.register(plugin, createMockManifest('test-plugin'));
expect(service.get('test-plugin')).toBe(plugin);
});
});