Relatude Help

Tests

Keep logic that does not need the interface in plain modules, and test it with Vitest. Vitest reads the project's Vite config and needs no setup of its own:

npm install -D vitest

src/stock.ts:

/** How a stock count is shown. */
export function stockLabel(count: number): string {
  if (count <= 0) return 'Sold out';
  return count < 5 ? `Only ${count} left` : 'In stock';
}

src/stock.test.ts:

import { describe, expect, it } from 'vitest';
import { stockLabel } from './stock';

describe('stockLabel', () => {
  it('says when little is left', () => {
    expect(stockLabel(0)).toBe('Sold out');
    expect(stockLabel(3)).toBe('Only 3 left');
    expect(stockLabel(12)).toBe('In stock');
  });
});

npx vitest runs the tests on every save, and npx vitest run runs them once. Test files never end up in the plugin, since the build takes only what src/index.tsx imports. Try components in the CMS itself, with the dev server.