Skip to content
Last updated Give Feedback

Testing Plugins

The @ecommus/testing package provides mock factories and a test app harness for testing plugins and custom business logic.

Terminal window
npm install --save-dev @ecommus/testing

Or if you are working in the monorepo, it is already available.

import { createMockOrder } from '@ecommus/testing';
const order = createMockOrder({
total: 15000, // in smallest currency unit (bani)
currency: 'RON',
status: 'paid',
items: [
{ productId: 'prod_1', qty: 2, price: 7500 }
]
});

Partial fields are merged with sensible defaults. All generated IDs use the ecommus cuid2 format.

import { createMockProduct } from '@ecommus/testing';
const product = createMockProduct({
name: 'Test Product',
price: 9900,
stock: 10
});
import { createMockCustomer } from '@ecommus/testing';
const customer = createMockCustomer({
email: 'test@example.com',
tags: ['vip']
});
import { createMockTenant } from '@ecommus/testing';
const tenant = createMockTenant({ slug: 'test-store' });

createTestApp bootstraps a real Fastify instance with an in-memory pglite database — ideal for integration tests.

import { createTestApp, createMockTenant } from '@ecommus/testing';
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
describe('My Plugin Integration', () => {
let app: Awaited<ReturnType<typeof createTestApp>>;
beforeAll(async () => {
app = await createTestApp({
plugins: ['../plugins/my-plugin/src/index.ts']
});
});
afterAll(async () => {
await app.close();
});
it('registers its routes', async () => {
const res = await app.inject({
method: 'GET',
url: '/api/my-plugin/health',
});
expect(res.statusCode).toBe(200);
});
});

For pure business logic in packages/core, test functions directly with vitest:

import { describe, it, expect } from 'vitest';
import { computeItemTotal } from '@ecommus/core';
describe('computeItemTotal', () => {
it('applies quantity discount', () => {
const result = computeItemTotal({ price: 1000, qty: 3, discount: 0.1 });
expect(result).toBe(2700); // 3 × 1000 × 0.9
});
});
Terminal window
# All tests in monorepo
npx vitest run
# Only a specific package
npx vitest run --project packages/core
# With coverage
npx vitest run --coverage

Tests run automatically on every push via .github/workflows/ci.yml:

- run: npx vitest run
working-directory: packages/core

The @ecommus/testing package is also used in the CI pipeline to validate plugin compatibility across versions.