Syntax previewClick to edit
1234
export default async function measure(fn, now = () => Date.now()) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
12345678910111213141516171819202122232425262728293031
import measure from './measure';
describe('measure', () => {
test('measures sync functions', async () => {
let t = 0;
const now = () => (t += 5);
const { result, durationMs } = await measure(() => 42, now);
expect(result).toBe(42);
expect(durationMs).toBe(5);
});
test('measures async functions', async () => {
let t = 100;
const now = () => (t += 10);
const { result, durationMs } = await measure(async () => 'ok', now);
expect(result).toBe('ok');
expect(durationMs).toBe(10);
});
test('propagates errors', async () => {
const now = () => 1;
await expect(measure(() => { throw new Error('boom'); }, now)).rejects.toThrow('boom');
});
test('propagates async rejections', async () => {
const now = () => 1;
await expect(measure(async () => { throw new Error('async boom'); }, now)).rejects.toThrow('async boom');
});
});
Run tests to see results.