Performance: Testing server actions
Testing server actions impacts the speed and reliability of backend logic execution, indirectly affecting user experience by ensuring fast and correct server responses.
import { serverAction } from './actions'; jest.mock('./actions', () => ({ serverAction: jest.fn().mockResolvedValue('expected') })); test('server action fast mock test', async () => { const result = await serverAction(); expect(result).toBe('expected'); });
import { serverAction } from './actions'; test('server action slow test', async () => { const result = await serverAction(); expect(result).toBe('expected'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Real server action calls in tests | 0 (server side) | 0 | 0 | [✗] Bad |
| Mocked server actions in tests | 0 (server side) | 0 | 0 | [✓] Good |
import { describe, it, expect } from 'vitest';
import { addNumbers } from '@/app/actions';
describe('addNumbers', () => {
it('adds two numbers correctly', () => {
const result = addNumbers(2, 3);
expect(result).toBe(5);
});
});import { it, expect } from 'vitest';
import { fetchData } from '@/app/actions';
it('fetches data successfully', () => {
const data = fetchData();
expect(data).toHaveProperty('id');
});Pass the test and this pattern moves unknown