Syntax previewClick to edit
1234
export default async function runWithPerfBudget(fn, budgetMs, now) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
123456789101112131415161718192021222324252627282930313233343536
import runWithPerfBudget from './runWithPerfBudget';
describe('runWithPerfBudget', () => {
test('times sync functions and returns ok status', async () => {
const times = [100, 112];
const now = () => times.shift();
const res = await runWithPerfBudget(() => 123, 20, now);
expect(res).toEqual({ ok: true, durationMs: 12, value: 123 });
});
test('times async functions and awaits the result', async () => {
const times = [0, 50];
const now = () => times.shift();
const res = await runWithPerfBudget(async () => 'ok', 40, now);
expect(res.ok).toBe(false);
expect(res.durationMs).toBe(50);
expect(res.value).toBe('ok');
});
test('clamps negative duration to 0', async () => {
const times = [10, 5];
const now = () => times.shift();
const res = await runWithPerfBudget(() => 'x', 0, now);
expect(res.durationMs).toBe(0);
expect(res.ok).toBe(true);
});
test('throws on invalid inputs', async () => {
await expect(runWithPerfBudget(null, 1)).rejects.toThrow();
await expect(runWithPerfBudget(() => 1, -1)).rejects.toThrow();
});
});
Run tests to see results.