diff --git a/src/elapsed.js b/src/elapsed.js index 4991911..af1585f 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -25,14 +25,13 @@ /** * @todo #368:30min Decide the proper location for the `elapsed` utility function. * - Consider if this method belong is in the right place. - * It might belong in a utility module. - * For now, it remains here. - * - * - Review if the test file for this method is appropriately located, as its current location might not be ideal. + * It might belong in a utility module. For now, it remains here. + * Review if the test file for this method is appropriately located, + * as its current location might not be ideal. */ /** - * A utility function to measure the elapsed time of a task and provide + * A utility function to measure the elapsed time of a task and provideƏ * detailed timing information. * * This function wraps a given task (callback function) and provides it with @@ -47,21 +46,21 @@ * `task` callback will be returned unchanged. */ module.exports.elapsed = function elapsed(task) { - const startTime = Date.now(); - return task({ - print: (message) => { - const duration = Date.now() - startTime; - let extended; - if (duration < 1000) { - extended = `${duration}ms`; - } else if (duration < 60 * 1000) { - extended = `${Math.ceil(duration / 1000)}s`; - } else { - extended = `${Math.ceil(duration / 3600000)}min`; - } - const msg = `${message} in ${extended}`; - console.info(msg); - return msg; - } - }); -} + const startTime = Date.now(); + return task({ + print: (message) => { + const duration = Date.now() - startTime; + let extended; + if (duration < 1000) { + extended = `${duration}ms`; + } else if (duration < 60 * 1000) { + extended = `${Math.ceil(duration / 1000)}s`; + } else { + extended = `${Math.ceil(duration / 3600000)}min`; + } + const msg = `${message} in ${extended}`; + console.info(msg); + return msg; + } + }); +}; diff --git a/test/test_elapsed.js b/test/test_elapsed.js index ac33272..25146fc 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -22,52 +22,53 @@ * SOFTWARE. */ -const {elapsed} = require('../src/elapsed') -const assert = require("assert"); +const {elapsed} = require('../src/elapsed'); +const assert = require('assert'); -describe('elapsed', function(){ - const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); - it('measures time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(300); - return tracked.print("task"); - }) .then( - (actual)=> assert( - /task in 30\d+ms/.test(actual), - `Expected "${actual}" to match /task in 30\\d+ms/` - ) - ) - }); - it('measures short time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(10); - return tracked.print("short task"); - }) .then( - (actual) => assert( - /short task in 1\d+ms/.test(actual), - `Expected "${actual}" to match /short task in 1\\d+ms/` - ) - ); - }); - it('measures long time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(1200); - return tracked.print("long task"); - }) .then( - (actual) => assert( - /long task in 2s/.test(actual), - `Expected "${actual}" to match /long task in 2s/` - ) - ); - }); - it('handles errors in task correctly', async () => { - try { - await elapsed(async (tracked) => { - throw new Error("task error"); - }); - assert.fail("Expected an error to be thrown"); - } catch (error) { - assert.throws(() => { throw error }, /task error/); - } - }); -}) \ No newline at end of file +describe('elapsed', function() { + const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + it('measures time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(300); + return tracked.print('task'); + }) .then( + (actual)=> assert( + /task in 30\d+ms/.test(actual), + `Expected "${actual}" to match /task in 30\\d+ms/` + ) + ); + }); + it('measures short time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(10); + return tracked.print('short task'); + }) .then( + (actual) => assert( + /short task in 1\d+ms/.test(actual), + `Expected "${actual}" to match /short task in 1\\d+ms/` + ) + ); + }); + it('measures long time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(1200); + return tracked.print('long task'); + }) .then( + (actual) => assert( + /long task in 2s/.test(actual), + `Expected "${actual}" to match /long task in 2s/` + ) + ); + }); + it('handles errors in task correctly', async () => { + await assert.rejects( + elapsed(async () => { + throw new Error("task error"); + }), + (error) => { + assert.throws(() => { throw error }, /task error/); + return true; + } + ); + }); +});