Skip to content

Commit

Permalink
fix(objectionary#368): formatted, simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
trapvpack committed Nov 25, 2024
1 parent 42858f3 commit 357691e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
45 changes: 22 additions & 23 deletions src/elapsed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
});
};
97 changes: 49 additions & 48 deletions test/test_elapsed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
}
});
})
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;
}
);
});
});

0 comments on commit 357691e

Please sign in to comment.