Loading test/assignment-5/test_37.js +22 −3 Original line number Diff line number Diff line import delay from '../solution_37.js'; import assert from 'assert'; import sinon from 'sinon'; describe('solution_37', () => { describe('delay(ms)', () => { let clock; beforeEach(() => { clock = sinon.useFakeTimers(); }); afterEach(() => { clock.restore(); }); it('should return a Promise', () => { const result = delay(100); assert.strictEqual(typeof result.then, 'function'); Loading @@ -10,25 +21,32 @@ describe('solution_37', () => { it('should resolve after the specified delay', async () => { const start = Date.now(); await delay(200); const promise = delay(200); clock.tick(200); await promise; const elapsed = Date.now() - start; assert(elapsed >= 200, `Elapsed: ${elapsed}`); }); it('should resolve with undefined', async () => { const value = await delay(50); const promise = delay(50); clock.tick(50); const value = await promise; assert.strictEqual(value, undefined); }); it('should work with zero delay', async () => { const start = Date.now(); await delay(0); const promise = delay(0); clock.tick(0); await promise; const elapsed = Date.now() - start; assert(elapsed < 50, `Elapsed: ${elapsed}`); }); it('should accept then', (done) => { delay(10).then(() => done()); clock.tick(10); }); it('should allow chaining with then', (done) => { Loading @@ -38,6 +56,7 @@ describe('solution_37', () => { assert.strictEqual(val, 'next'); done(); }); clock.tick(10); }); }); }); test/assignment-5/test_38.js +28 −23 Original line number Diff line number Diff line import showSequenceMessages from '../solution_38.js'; import assert from 'assert'; import sinon from 'sinon'; describe('solution_38', () => { describe('showSequenceMessages()', () => { it('should print messages in order and respect delays', async function () { this.timeout(5000); // Allow enough time for all delays let clock; let logs = []; let originalLog; const logs = []; const originalLog = console.log; console.log = (msg) => logs.push({ msg, time: Date.now() }); const start = Date.now(); await showSequenceMessages(); beforeEach(() => { clock = sinon.useFakeTimers(); logs = []; originalLog = console.log; console.log = (msg) => logs.push({ msg, time: clock.now }); }); // Wait for all messages to be printed await new Promise((res) => setTimeout(res, 4200)); afterEach(() => { console.log = originalLog; clock.restore(); }); it('should print messages in order and respect delays', async () => { const promise = showSequenceMessages(); // Advance time for each expected message await clock.tickAsync(1000); // "Starting..." await clock.tickAsync(2000); // "Completed!" await clock.tickAsync(1000); // "End of messages." await promise; assert.strictEqual(logs.length, 3); assert.strictEqual(logs[0].msg, 'Starting...'); assert.strictEqual(logs[1].msg, 'Completed!'); assert.strictEqual(logs[2].msg, 'End of messages.'); // Check timings (allow some tolerance) assert.ok( logs[0].time - start >= 1000 - 150 && logs[0].time - start <= 1000 + 150, ); assert.ok( logs[1].time - logs[0].time >= 2000 - 150 && logs[1].time - logs[0].time <= 2000 + 150, ); assert.ok( logs[2].time - logs[1].time >= 1000 - 150 && logs[2].time - logs[1].time <= 1000 + 150, ); // Check timings assert.strictEqual(logs[0].time, 1000); assert.strictEqual(logs[1].time, 3000); assert.strictEqual(logs[2].time, 4000); }); }); }); Loading
test/assignment-5/test_37.js +22 −3 Original line number Diff line number Diff line import delay from '../solution_37.js'; import assert from 'assert'; import sinon from 'sinon'; describe('solution_37', () => { describe('delay(ms)', () => { let clock; beforeEach(() => { clock = sinon.useFakeTimers(); }); afterEach(() => { clock.restore(); }); it('should return a Promise', () => { const result = delay(100); assert.strictEqual(typeof result.then, 'function'); Loading @@ -10,25 +21,32 @@ describe('solution_37', () => { it('should resolve after the specified delay', async () => { const start = Date.now(); await delay(200); const promise = delay(200); clock.tick(200); await promise; const elapsed = Date.now() - start; assert(elapsed >= 200, `Elapsed: ${elapsed}`); }); it('should resolve with undefined', async () => { const value = await delay(50); const promise = delay(50); clock.tick(50); const value = await promise; assert.strictEqual(value, undefined); }); it('should work with zero delay', async () => { const start = Date.now(); await delay(0); const promise = delay(0); clock.tick(0); await promise; const elapsed = Date.now() - start; assert(elapsed < 50, `Elapsed: ${elapsed}`); }); it('should accept then', (done) => { delay(10).then(() => done()); clock.tick(10); }); it('should allow chaining with then', (done) => { Loading @@ -38,6 +56,7 @@ describe('solution_37', () => { assert.strictEqual(val, 'next'); done(); }); clock.tick(10); }); }); });
test/assignment-5/test_38.js +28 −23 Original line number Diff line number Diff line import showSequenceMessages from '../solution_38.js'; import assert from 'assert'; import sinon from 'sinon'; describe('solution_38', () => { describe('showSequenceMessages()', () => { it('should print messages in order and respect delays', async function () { this.timeout(5000); // Allow enough time for all delays let clock; let logs = []; let originalLog; const logs = []; const originalLog = console.log; console.log = (msg) => logs.push({ msg, time: Date.now() }); const start = Date.now(); await showSequenceMessages(); beforeEach(() => { clock = sinon.useFakeTimers(); logs = []; originalLog = console.log; console.log = (msg) => logs.push({ msg, time: clock.now }); }); // Wait for all messages to be printed await new Promise((res) => setTimeout(res, 4200)); afterEach(() => { console.log = originalLog; clock.restore(); }); it('should print messages in order and respect delays', async () => { const promise = showSequenceMessages(); // Advance time for each expected message await clock.tickAsync(1000); // "Starting..." await clock.tickAsync(2000); // "Completed!" await clock.tickAsync(1000); // "End of messages." await promise; assert.strictEqual(logs.length, 3); assert.strictEqual(logs[0].msg, 'Starting...'); assert.strictEqual(logs[1].msg, 'Completed!'); assert.strictEqual(logs[2].msg, 'End of messages.'); // Check timings (allow some tolerance) assert.ok( logs[0].time - start >= 1000 - 150 && logs[0].time - start <= 1000 + 150, ); assert.ok( logs[1].time - logs[0].time >= 2000 - 150 && logs[1].time - logs[0].time <= 2000 + 150, ); assert.ok( logs[2].time - logs[1].time >= 1000 - 150 && logs[2].time - logs[1].time <= 1000 + 150, ); // Check timings assert.strictEqual(logs[0].time, 1000); assert.strictEqual(logs[1].time, 3000); assert.strictEqual(logs[2].time, 4000); }); }); });