@ericcornelissen/assert-time 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +63 -0
  3. package/index.js +75 -0
  4. package/package.json +33 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 Eric Cornelissen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # `assert-time`
2
+
3
+ Assert the duration of a (blocking or async) function under test.
4
+
5
+ Note that blocking tests still block the event loop, it is just asserted that
6
+ it did not take longer than expected.
7
+
8
+ ## Usage
9
+
10
+ ### Generic Example
11
+
12
+ ```javascript
13
+ var assertTime = require('@ericcornelissen/assert-time');
14
+
15
+ var fut = require('./index.js');
16
+
17
+ // will throw if slow
18
+ assertTime(fut, 100);
19
+
20
+ // will call onSlow or onTime
21
+ assertTime(
22
+ fut,
23
+ 100,
24
+ function onSlow(duration) {
25
+ // ...
26
+ },
27
+ function onTime(duration) {
28
+ // ...
29
+ }
30
+ );
31
+ ```
32
+
33
+ ### [Tape]
34
+
35
+ ```javascript
36
+ var test = require('tape');
37
+ var assertTime = require('@ericcornelissen/assert-time');
38
+
39
+ var fut = require('./index.js');
40
+
41
+ test('timing test #1', function (t) {
42
+ t.doesNotThrow(function () {
43
+ assertTime(fut, timeout);
44
+ });
45
+ t.end();
46
+ });
47
+
48
+ test('timing test #2', function (t) {
49
+ assertTime(
50
+ fut,
51
+ 100,
52
+ function onSlow(duration) {
53
+ t.fail('Test timed out after ' + duration + 'ms');
54
+ t.end();
55
+ },
56
+ function onTime() {
57
+ t.end();
58
+ }
59
+ );
60
+ });
61
+ ```
62
+
63
+ [tape]: https://www.npmjs.com/package/tape
package/index.js ADDED
@@ -0,0 +1,75 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ /**
4
+ * @param {Function} fut The function under test.
5
+ * @param {number} timeout The maximum runtime of `fn` in milliseconds.
6
+ * @param {function(number): void} [onSlow] The function called if `fn`'s runtime exceeds `timeout`, receives the runtime in milliseconds.
7
+ * @param {function(number): void} [onTime] The function called if `fn`'s runtime is within `timeout`, receives the runtime in milliseconds.
8
+ * @throws {number | undefined | Promise<number> | Promise<undefined>} If `onSlow` is not provied `fn`'s duration, otherwise `undefined`.
9
+ * @throws {Error} If `onSlow` is not provied `fn`'s duration exceeds `timeout`.
10
+ */
11
+ function assertTime(fut, timeout, onSlow, onTime) {
12
+ var t1, t2, duration;
13
+ var timedOut = false;
14
+
15
+ if (typeof onSlow === 'function') {
16
+ t1 = setTimeout(function () {
17
+ timedOut = true;
18
+
19
+ clearTimeout(t2);
20
+ onSlow(duration || timeout);
21
+ }, timeout);
22
+ }
23
+
24
+ var start = Date.now();
25
+ const result = fut();
26
+ if (typeof onSlow === 'function') {
27
+ if (isPromise(result)) {
28
+ result.then(function () {
29
+ duration = Date.now() - start;
30
+ if (!timedOut) {
31
+ clearTimeout(t1);
32
+ onTime(duration);
33
+ }
34
+
35
+ return undefined;
36
+ });
37
+ } else {
38
+ duration = Date.now() - start;
39
+ t2 = setTimeout(function () {
40
+ if (duration < timeout) {
41
+ clearTimeout(t1);
42
+ onTime(duration);
43
+ }
44
+ }, 0);
45
+ }
46
+ } else {
47
+ if (isPromise(result)) {
48
+ return result.then(function () {
49
+ duration = Date.now() - start;
50
+ if (duration > timeout) {
51
+ throw new Error('Timeout of ' + timeout + 'ms exceeded (took ' + duration + 'ms)');
52
+ } else {
53
+ return duration;
54
+ }
55
+ });
56
+ } else {
57
+ duration = Date.now() - start;
58
+ if (duration > timeout) {
59
+ throw new Error('Timeout of ' + timeout + 'ms exceeded (took ' + duration + 'ms)');
60
+ } else {
61
+ return duration;
62
+ }
63
+ }
64
+ }
65
+ }
66
+
67
+ function isPromise(v) {
68
+ try {
69
+ return v instanceof Promise;
70
+ } catch (_) {
71
+ return false;
72
+ }
73
+ }
74
+
75
+ module.exports = assertTime;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@ericcornelissen/assert-time",
3
+ "version": "1.0.0",
4
+ "description": "assert the duration of a (blocking or async) function under test",
5
+ "type": "commonjs",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Eric Cornelissen",
9
+ "email": "ericornelissen@gmail.com",
10
+ "url": "https://ericcornelissen.dev/"
11
+ },
12
+ "engines": {
13
+ "node": ">=0.8.28"
14
+ },
15
+ "exports": {
16
+ ".": "./index.js"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/ericcornelissen/node-assert-time.git"
21
+ },
22
+ "scripts": {
23
+ "example:mocha": "mocha example-mocha.js",
24
+ "example:tape": "node example-tape.js",
25
+ "test": "node test.js",
26
+ "test:compatibility": "nve 0.8.28,0.10.31,0.12.0,4.0.0,5.0.0,6.0.0,7.0.0,8.0.0,9.0.0,10.0.0,11.0.0,12.0.0,13.0.0,14.0.0,15.0.0,16.0.0,17.0.0,18.0.0,19.0.0,20.0.0,21.0.0,22.0.0,23.0.0,24.0.0,25.0.0,26.0.0 node test.js"
27
+ },
28
+ "devDependencies": {
29
+ "mocha": "11.7.6",
30
+ "nve": "18.0.3",
31
+ "tape": "5.9.0"
32
+ }
33
+ }