FrontendAtlas
Study
▾
Dashboard
Pricing
Get full access
</> Code
Test cases
Language
JavaScript
TypeScript
export default function once(fn) { throw new Error('Not implemented'); }
import once from './file'; describe('once', () => { test('calls the function only once', () => { let calls = 0; const fn = once(() => { calls += 1; return calls; }); expect(fn()).toBe(1); expect(fn()).toBe(1); expect(calls).toBe(1); }); test('returns the first result on later calls', () => { const add = once((a, b) => a + b); expect(add(1, 2)).toBe(3); expect(add(5, 6)).toBe(3); }); test('preserves this context', () => { const obj = { x: 2, get: once(function (n) { return this.x + n; }) }; expect(obj.get(3)).toBe(5); }); });
▶ Run tests
Results
Console
Run tests to see results.