Testing Plugins
Testing Plugins
Section titled “Testing Plugins”The @ecommus/testing package provides mock factories and a test app harness for testing plugins and custom business logic.
Installation
Section titled “Installation”npm install --save-dev @ecommus/testingOr if you are working in the monorepo, it is already available.
Mock Factories
Section titled “Mock Factories”createMockOrder
Section titled “createMockOrder”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.
createMockProduct
Section titled “createMockProduct”import { createMockProduct } from '@ecommus/testing';
const product = createMockProduct({ name: 'Test Product', price: 9900, stock: 10});createMockCustomer
Section titled “createMockCustomer”import { createMockCustomer } from '@ecommus/testing';
const customer = createMockCustomer({ email: 'test@example.com', tags: ['vip']});createMockTenant
Section titled “createMockTenant”import { createMockTenant } from '@ecommus/testing';
const tenant = createMockTenant({ slug: 'test-store' });Test App Harness
Section titled “Test App Harness”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); });});Unit Testing Business Logic
Section titled “Unit Testing Business Logic”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 });});Running Tests
Section titled “Running Tests”# All tests in monoreponpx vitest run
# Only a specific packagenpx vitest run --project packages/core
# With coveragenpx vitest run --coverageCI Integration
Section titled “CI Integration”Tests run automatically on every push via .github/workflows/ci.yml:
- run: npx vitest run working-directory: packages/coreThe @ecommus/testing package is also used in the CI pipeline to validate plugin compatibility across versions.