@ac-kit/test-util 0.1.2 → 0.2.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.
- package/CHANGELOG.md +21 -0
- package/README.md +1 -1
- package/dist/fake-clock.d.ts +28 -0
- package/dist/fake-clock.d.ts.map +1 -0
- package/dist/fake-clock.js +57 -0
- package/dist/fake-clock.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @ac-kit/test-util
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 235ec3c: `Clock` names a time source with a one-shot timer, so scheduling code can run
|
|
8
|
+
on a fake clock; `SYSTEM_CLOCK` is the real one, on `performance.now()` and
|
|
9
|
+
`setTimeout`. `@ac-kit/test-util`'s `FakeClock` only moves when told to and
|
|
10
|
+
fires due timers in time order.
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [235ec3c]
|
|
15
|
+
- @ac-kit/core@0.5.0
|
|
16
|
+
|
|
17
|
+
## 0.1.3
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies [aca1cb1]
|
|
22
|
+
- @ac-kit/core@0.4.0
|
|
23
|
+
|
|
3
24
|
## 0.1.2
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -25,5 +25,5 @@ TCP/TLS endpoints), see `@ac-kit/integration-test-util` instead — that package
|
|
|
25
25
|
is allowed heavier dependencies precisely because its own consumers are few.
|
|
26
26
|
|
|
27
27
|
See the
|
|
28
|
-
[generated API docs](https://anthochamp.
|
|
28
|
+
[generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/test-util/)
|
|
29
29
|
for the full reference.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Clock } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* A {@link Clock} that only moves when told to, firing due timers in time order,
|
|
4
|
+
* each with `now()` at its own time: the clock for testing code that schedules
|
|
5
|
+
* work, without fake global timers.
|
|
6
|
+
*
|
|
7
|
+
* Timers set while advancing are honoured in the same advance when due.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const clock = new FakeClock();
|
|
11
|
+
* clock.setTimer(100, () => console.log(clock.now())); // logs 100
|
|
12
|
+
* clock.advance(250);
|
|
13
|
+
* clock.now(); // 250
|
|
14
|
+
*/
|
|
15
|
+
export declare class FakeClock implements Clock {
|
|
16
|
+
private time;
|
|
17
|
+
private readonly timers;
|
|
18
|
+
private nextId;
|
|
19
|
+
/** @param start - The time to start at, 0 by default. */
|
|
20
|
+
constructor(start?: number);
|
|
21
|
+
now(): number;
|
|
22
|
+
setTimer(at: number, callback: () => void): () => void;
|
|
23
|
+
/** Moves time forward by `milliseconds`, firing every timer due on the way. */
|
|
24
|
+
advance(milliseconds: number): void;
|
|
25
|
+
/** Moves time to `end`, firing every timer due on the way; never backwards. */
|
|
26
|
+
advanceTo(end: number): void;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=fake-clock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-clock.d.ts","sourceRoot":"","sources":["../src/fake-clock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE1C;;;;;;;;;;;;GAYG;AACH,qBAAa,SAAU,YAAW,KAAK;IACtC,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAGnB;IACJ,OAAO,CAAC,MAAM,CAAK;IAEnB,yDAAyD;IACzD,YAAY,KAAK,SAAI,EAEpB;IAED,GAAG,IAAI,MAAM,CAEZ;IAED,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAKrD;IAED,+EAA+E;IAC/E,OAAO,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAElC;IAED,+EAA+E;IAC/E,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAqB3B;CACD"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A {@link Clock} that only moves when told to, firing due timers in time order,
|
|
3
|
+
* each with `now()` at its own time: the clock for testing code that schedules
|
|
4
|
+
* work, without fake global timers.
|
|
5
|
+
*
|
|
6
|
+
* Timers set while advancing are honoured in the same advance when due.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const clock = new FakeClock();
|
|
10
|
+
* clock.setTimer(100, () => console.log(clock.now())); // logs 100
|
|
11
|
+
* clock.advance(250);
|
|
12
|
+
* clock.now(); // 250
|
|
13
|
+
*/
|
|
14
|
+
export class FakeClock {
|
|
15
|
+
time;
|
|
16
|
+
timers = new Map();
|
|
17
|
+
nextId = 0;
|
|
18
|
+
/** @param start - The time to start at, 0 by default. */
|
|
19
|
+
constructor(start = 0) {
|
|
20
|
+
this.time = start;
|
|
21
|
+
}
|
|
22
|
+
now() {
|
|
23
|
+
return this.time;
|
|
24
|
+
}
|
|
25
|
+
setTimer(at, callback) {
|
|
26
|
+
const id = this.nextId++;
|
|
27
|
+
this.timers.set(id, { at, callback });
|
|
28
|
+
return () => this.timers.delete(id);
|
|
29
|
+
}
|
|
30
|
+
/** Moves time forward by `milliseconds`, firing every timer due on the way. */
|
|
31
|
+
advance(milliseconds) {
|
|
32
|
+
this.advanceTo(this.time + milliseconds);
|
|
33
|
+
}
|
|
34
|
+
/** Moves time to `end`, firing every timer due on the way; never backwards. */
|
|
35
|
+
advanceTo(end) {
|
|
36
|
+
for (;;) {
|
|
37
|
+
let dueId;
|
|
38
|
+
let dueAt = Infinity;
|
|
39
|
+
// Ties fire in the order they were set: ids grow with insertion.
|
|
40
|
+
for (const [id, { at }] of this.timers) {
|
|
41
|
+
if (at <= end && at < dueAt) {
|
|
42
|
+
dueId = id;
|
|
43
|
+
dueAt = at;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (dueId === undefined) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
const { callback } = this.timers.get(dueId);
|
|
50
|
+
this.timers.delete(dueId);
|
|
51
|
+
this.time = Math.max(this.time, dueAt);
|
|
52
|
+
callback();
|
|
53
|
+
}
|
|
54
|
+
this.time = Math.max(this.time, end);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=fake-clock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-clock.js","sourceRoot":"","sources":["../src/fake-clock.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,SAAS;IACb,IAAI,CAAS;IACJ,MAAM,GAAG,IAAI,GAAG,EAG9B,CAAC;IACI,MAAM,GAAG,CAAC,CAAC;IAEnB,yDAAyD;IACzD,YAAY,KAAK,GAAG,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IACnB,CAAC;IAED,GAAG;QACF,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IAED,QAAQ,CAAC,EAAU,EAAE,QAAoB;QACxC,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QACzB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC;QAEtC,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;IAED,+EAA+E;IAC/E,OAAO,CAAC,YAAoB;QAC3B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC;IAC1C,CAAC;IAED,+EAA+E;IAC/E,SAAS,CAAC,GAAW;QACpB,SAAS,CAAC;YACT,IAAI,KAAyB,CAAC;YAC9B,IAAI,KAAK,GAAG,QAAQ,CAAC;YACrB,iEAAiE;YACjE,KAAK,MAAM,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;gBACxC,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,GAAG,KAAK,EAAE,CAAC;oBAC7B,KAAK,GAAG,EAAE,CAAC;oBACX,KAAK,GAAG,EAAE,CAAC;gBACZ,CAAC;YACF,CAAC;YACD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACzB,MAAM;YACP,CAAC;YAED,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC;YAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACvC,QAAQ,EAAE,CAAC;QACZ,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACtC,CAAC;CACD"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/test-util",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Dependency-free test-harness primitives, such as a deterministic synchronous delay for timing-sensitive tests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"test-utils",
|
|
7
7
|
"testing",
|
|
8
8
|
"vitest"
|
|
9
9
|
],
|
|
10
|
-
"homepage": "https://anthochamp.
|
|
10
|
+
"homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/test-util/",
|
|
11
11
|
"bugs": {
|
|
12
|
-
"url": "https://
|
|
12
|
+
"url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
|
|
13
13
|
},
|
|
14
14
|
"license": "BSD-3-Clause",
|
|
15
15
|
"author": "Anthony Champagne <dev@anthonychampagne.fr>",
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
|
-
"url": "git+https://
|
|
18
|
+
"url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
|
|
19
19
|
"directory": "packages/test-util"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
@@ -37,19 +37,19 @@
|
|
|
37
37
|
"prepack": "yarn build"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@ac-kit/core": "^0.
|
|
40
|
+
"@ac-kit/core": "^0.5.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@ac-kit/tsconfig": "0.1.0",
|
|
44
44
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
45
45
|
"@ac-kit/vitest-config": "0.1.1",
|
|
46
|
-
"@types/node": "26.5
|
|
46
|
+
"@types/node": "26.6.5",
|
|
47
47
|
"@typescript/native": "typescript@7.0.2",
|
|
48
48
|
"rimraf": "6.1.3",
|
|
49
49
|
"typedoc": "0.28.20",
|
|
50
50
|
"typescript": "@typescript/typescript6@6.0.2",
|
|
51
|
-
"vite": "8.3.
|
|
52
|
-
"vitest": "5.0.
|
|
51
|
+
"vite": "8.3.5",
|
|
52
|
+
"vitest": "5.0.5"
|
|
53
53
|
},
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": ">=24"
|