@ericcornelissen/assert-time 1.0.0 → 1.0.1

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 (3) hide show
  1. package/README.md +49 -8
  2. package/index.js +53 -46
  3. package/package.json +5 -3
package/README.md CHANGED
@@ -13,14 +13,15 @@ it did not take longer than expected.
13
13
  var assertTime = require('@ericcornelissen/assert-time');
14
14
 
15
15
  var fut = require('./index.js');
16
+ var timeout = 100;
16
17
 
17
18
  // will throw if slow
18
- assertTime(fut, 100);
19
+ assertTime(fut, timeout);
19
20
 
20
- // will call onSlow or onTime
21
+ // will call `onSlow` if slow, `onTime` otherwise
21
22
  assertTime(
22
23
  fut,
23
- 100,
24
+ timeout,
24
25
  function onSlow(duration) {
25
26
  // ...
26
27
  },
@@ -30,25 +31,59 @@ assertTime(
30
31
  );
31
32
  ```
32
33
 
33
- ### [Tape]
34
+ ### [AVA]
35
+
36
+ With [AVA], blocking tests don't cause tests to fail due to a timeout. Async
37
+ tests do.
38
+
39
+ ```javascript
40
+ var test = require('ava').default; // ^8
41
+ var assertTime = require('@ericcornelissen/assert-time');
42
+
43
+ var fut = require('./index.js');
44
+ var timeout = 100;
45
+
46
+ test('timing test, variant #1', function (t) {
47
+ // Causes an assertion error
48
+ t.doesNotThrow(function () {
49
+ assertTime(fut, timeout);
50
+ });
51
+ });
52
+
53
+ test('timing test, variant #2', function (t) {
54
+ // Causes a test error
55
+ assertTime(fut, timeout);
56
+ t.pass();
57
+ });
58
+ ```
59
+
60
+ [ava]: https://www.npmjs.com/package/ava
61
+
62
+ ### [tape]
63
+
64
+ With [tape], blocking tests don't cause tests to fail due to a timeout. Async
65
+ tests do.
34
66
 
35
67
  ```javascript
36
- var test = require('tape');
68
+ var test = require('tape'); // ^5
37
69
  var assertTime = require('@ericcornelissen/assert-time');
38
70
 
39
71
  var fut = require('./index.js');
72
+ var timeout = 100;
40
73
 
41
- test('timing test #1', function (t) {
74
+ test('timing test, variant #1', function (t) {
75
+ // Causes a test error
42
76
  t.doesNotThrow(function () {
43
77
  assertTime(fut, timeout);
44
78
  });
45
79
  t.end();
46
80
  });
47
81
 
48
- test('timing test #2', function (t) {
82
+ test('timing test, variant #2', function (t) {
83
+ // Causes a test error
49
84
  assertTime(
50
85
  fut,
51
- 100,
86
+ timeout,
52
87
  function onSlow(duration) {
53
88
  t.fail('Test timed out after ' + duration + 'ms');
54
89
  t.end();
@@ -58,6 +93,12 @@ test('timing test #2', function (t) {
58
93
  }
59
94
  );
60
95
  });
96
+
97
+ test('timing test, variant #3', function (t) {
98
+ // Causes the test to error and the test suite to STOP
99
+ assertTime(fut, timeout);
100
+ t.end();
101
+ });
61
102
  ```
62
103
 
63
104
  [tape]: https://www.npmjs.com/package/tape
package/index.js CHANGED
@@ -2,66 +2,64 @@
2
2
 
3
3
  /**
4
4
  * @param {Function} fut The function under test.
5
- * @param {number} timeout The maximum runtime of `fn` in milliseconds.
5
+ * @param {number} deadline The maximum runtime of `fn` in milliseconds.
6
6
  * @param {function(number): void} [onSlow] The function called if `fn`'s runtime exceeds `timeout`, receives the runtime in milliseconds.
7
7
  * @param {function(number): void} [onTime] The function called if `fn`'s runtime is within `timeout`, receives the runtime in milliseconds.
8
8
  * @throws {number | undefined | Promise<number> | Promise<undefined>} If `onSlow` is not provied `fn`'s duration, otherwise `undefined`.
9
9
  * @throws {Error} If `onSlow` is not provied `fn`'s duration exceeds `timeout`.
10
+ * @throws {Error} If `onSlow` is not provided and `fn`'s duration exceeds `timeout`.
10
11
  */
11
- function assertTime(fut, timeout, onSlow, onTime) {
12
- var t1, t2, duration;
13
- var timedOut = false;
12
+ function assertTime(fut, deadline, onSlow, onTime) {
13
+ if (onSlow === undefined) {
14
+ return assertTimeThrowing(fut, deadline);
15
+ } else {
16
+ return assertTimeCallback(fut, deadline, onSlow, onTime);
17
+ }
18
+ }
14
19
 
15
- if (typeof onSlow === 'function') {
16
- t1 = setTimeout(function () {
17
- timedOut = true;
20
+ function assertTimeCallback(fut, deadline, onSlow, onTime) {
21
+ var timeout, duration;
22
+ var timedOut = false;
18
23
 
19
- clearTimeout(t2);
20
- onSlow(duration || timeout);
21
- }, timeout);
24
+ function check() {
25
+ duration = timer.end(start);
26
+ if (duration < deadline && !timedOut) {
27
+ clearTimeout(timeout);
28
+ onTime(duration);
29
+ }
22
30
  }
23
31
 
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
- }
32
+ timeout = setTimeout(function () {
33
+ timedOut = true;
34
+ onSlow(duration || deadline);
35
+ }, deadline);
34
36
 
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
- }
37
+ var start = timer.start();
38
+ var result = fut();
39
+ if (isPromise(result)) {
40
+ result.then(check);
46
41
  } 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
- });
42
+ check();
43
+ }
44
+ }
45
+
46
+ function assertTimeThrowing(fut, deadline) {
47
+ function check() {
48
+ var duration = timer.end(start);
49
+ if (duration >= deadline) {
50
+ throw new Error('Timeout of ' + deadline + 'ms exceeded (took ' + duration + 'ms)');
56
51
  } 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
- }
52
+ return duration;
63
53
  }
64
54
  }
55
+
56
+ var start = timer.start();
57
+ var result = fut();
58
+ if (isPromise(result)) {
59
+ return result.then(check);
60
+ } else {
61
+ return check();
62
+ }
65
63
  }
66
64
 
67
65
  function isPromise(v) {
@@ -72,4 +70,13 @@ function isPromise(v) {
72
70
  }
73
71
  }
74
72
 
73
+ const timer = {
74
+ start: function () {
75
+ return { __timestamp: Date.now() };
76
+ },
77
+ end: function (start) {
78
+ return Date.now() - start.__timestamp;
79
+ }
80
+ };
81
+
75
82
  module.exports = assertTime;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ericcornelissen/assert-time",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "assert the duration of a (blocking or async) function under test",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
@@ -10,7 +10,7 @@
10
10
  "url": "https://ericcornelissen.dev/"
11
11
  },
12
12
  "engines": {
13
- "node": ">=0.8.28"
13
+ "node": ">=0.8.6"
14
14
  },
15
15
  "exports": {
16
16
  ".": "./index.js"
@@ -20,12 +20,14 @@
20
20
  "url": "git+https://github.com/ericcornelissen/node-assert-time.git"
21
21
  },
22
22
  "scripts": {
23
+ "example:ava": "ava example-ava.js",
23
24
  "example:mocha": "mocha example-mocha.js",
24
25
  "example:tape": "node example-tape.js",
25
26
  "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
+ "test:compatibility": "nve 0.8.6,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
  },
28
29
  "devDependencies": {
30
+ "ava": "8.0.0",
29
31
  "mocha": "11.7.6",
30
32
  "nve": "18.0.3",
31
33
  "tape": "5.9.0"