@nestjsforge/echarts
Testing
Unit and integration testing strategies for @nestjsforge/echarts.
Unit testing with mocks
For unit tests, mock EChartsService to avoid spawning Puppeteer:
import { Test, TestingModule } from '@nestjs/testing';
import { EChartsService } from '@nestjsforge/echarts';
import { ChartsService } from './charts.service';
describe('ChartsService', () => {
let service: ChartsService;
let echarts: jest.Mocked<EChartsService>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ChartsService,
{
provide: EChartsService,
useValue: {
renderToBuffer: jest.fn().mockResolvedValue(Buffer.from('fake-png')),
renderToBase64: jest.fn().mockResolvedValue('ZmFrZS1wbmc='),
},
},
],
}).compile();
service = module.get(ChartsService);
echarts = module.get(EChartsService);
});
it('should render bar chart', async () => {
const buffer = await service.renderBarChart();
expect(echarts.renderToBuffer).toHaveBeenCalledTimes(1);
expect(Buffer.isBuffer(buffer)).toBe(true);
});
});Integration testing
For integration tests, use EChartsModule.forRoot with a single instance:
describe('EChartsService (integration)', () => {
let service: EChartsService;
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [EChartsModule.forRoot({ poolSize: 1, timeout: 30_000 })],
}).compile();
const app = module.createNestApplication();
await app.init();
service = module.get(EChartsService);
});
afterAll(async () => {
await app.close();
});
it('should render a real chart', async () => {
const buffer = await service.renderToBuffer({
xAxis: { type: 'category', data: ['A', 'B', 'C'] },
yAxis: { type: 'value' },
series: [{ data: [1, 2, 3], type: 'bar' }],
});
expect(Buffer.isBuffer(buffer)).toBe(true);
expect(buffer.length).toBeGreaterThan(1000);
});
});