Performance: Unit testing loaders and actions
Unit testing loaders and actions affects development speed and reliability but has minimal direct impact on page load or rendering performance.
import { loader } from './route'; import { mockDatabase } from './mocks'; test('loader returns mocked data', async () => { const context = { params: {}, context: { db: mockDatabase } }; const response = await loader(context); const data = await response.json(); expect(data).toEqual({ user: 'test' }); });
import { loader } from './route'; test('loader returns data', async () => { const response = await loader({ params: {} }); const data = await response.json(); expect(data).toBeDefined(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct loader call without mocks | 0 | 0 | 0 | [X] Bad |
| Loader call with mocked dependencies | 0 | 0 | 0 | [OK] Good |
Request object for testing an action in Remix?result contain after calling await loader({ request }) if the loader returns json({ message: 'Hello' })?
const request = new Request('http://localhost');
const result = await loader({ request });TypeError: request.formData is not a function. What is the most likely cause?Pass the test and this pattern moves unknown