Performance: Unit testing logic
Unit testing logic affects development speed and feedback loop but does not impact page load or rendering performance directly.
import { describe, it, expect } from 'vitest'; import { myLogicFunction } from './myLogic.js'; // Test logic directly without rendering describe('myLogicFunction', () => { it('returns correct result', () => { expect(myLogicFunction(5)).toBe(10); }); });
import { render } from '@testing-library/svelte'; import { test, expect } from 'vitest'; import MyComponent from './MyComponent.svelte'; // Test that relies on full DOM rendering test('checks logic by rendering full component', () => { const { getByText } = render(MyComponent); expect(getByText('Hello')).toBeInTheDocument(); // Additional logic tests done via DOM queries });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Testing logic via full component render | Many DOM nodes created | Multiple reflows triggered | High paint cost due to rendering | [X] Bad |
| Testing logic via direct function calls | No DOM nodes | No reflows | No paint cost | [OK] Good |
describe('sum function', () => {
it('adds two numbers', () => {
const sum = (a, b) => a + b;
expect(sum(2, 3)).toBe(5);
});
});describe('multiply function', () => {
it('multiplies two numbers', () => {
const multiply = (a, b) => a * b
expect(multiply(2, 3)).toBe(6)
}
});function checkPositive(n) {
return n > 0 ? 'positive' : 'not positive';
}Pass the test and this pattern moves unknown