jest-time-helpers 0.1.1 → 1.1.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/dist/index.d.ts +7 -3
- package/dist/index.js +62 -68
- package/dist/index.js.map +1 -1
- package/package.json +9 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
/**
|
|
3
2
|
* Wait a number of milliseconds of _real time_ (not mocked time); useful for
|
|
4
3
|
* allowing the runloop or external systems to advance.
|
|
@@ -20,6 +19,10 @@ export declare const sleep: (ms: number, unref?: boolean) => Promise<void> & {
|
|
|
20
19
|
* @param pollInterval - an optional period of how long we should wait between checks; lower values increase load on the server but may make tests pass faster, larger values are more efficient but increase test latency.
|
|
21
20
|
*/
|
|
22
21
|
export declare function sleepUntil(condition: () => boolean, maxDuration?: number, pollInterval?: number): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Returns the real-world current timestamp (unaffected by fake timers).
|
|
24
|
+
*/
|
|
25
|
+
declare function realNow(): number;
|
|
23
26
|
/**
|
|
24
27
|
* Sets up fake timers and Date implementation within your Jest test file. You
|
|
25
28
|
* should call this at the top of the file (**not** within a test or an
|
|
@@ -35,8 +38,8 @@ export declare function sleepUntil(condition: () => boolean, maxDuration?: numbe
|
|
|
35
38
|
* offset.
|
|
36
39
|
*/
|
|
37
40
|
export declare function setupFakeTimers(): {
|
|
38
|
-
setTime: (
|
|
39
|
-
realNow:
|
|
41
|
+
setTime: (targetTimestamp: number, increment?: number) => Promise<void>;
|
|
42
|
+
realNow: typeof realNow;
|
|
40
43
|
};
|
|
41
44
|
/** One second in milliseconds */
|
|
42
45
|
export declare const SECOND = 1000;
|
|
@@ -48,3 +51,4 @@ export declare const HOUR: number;
|
|
|
48
51
|
export declare const DAY: number;
|
|
49
52
|
/** One week in milliseconds */
|
|
50
53
|
export declare const WEEK: number;
|
|
54
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WEEK = exports.DAY = exports.HOUR = exports.MINUTE = exports.SECOND = exports.
|
|
4
|
-
|
|
5
|
-
|
|
36
|
+
exports.WEEK = exports.DAY = exports.HOUR = exports.MINUTE = exports.SECOND = exports.sleep = void 0;
|
|
37
|
+
exports.sleepUntil = sleepUntil;
|
|
38
|
+
exports.setupFakeTimers = setupFakeTimers;
|
|
39
|
+
const assert = __importStar(require("assert"));
|
|
40
|
+
const util = __importStar(require("util"));
|
|
6
41
|
// Grab the setTimeout from global before jest overwrites it with useFakeTimers
|
|
7
42
|
const setTimeoutBypassingFakes = global.setTimeout;
|
|
8
43
|
/**
|
|
@@ -42,7 +77,7 @@ async function sleepUntil(condition, maxDuration = 2000, pollInterval = 2) {
|
|
|
42
77
|
// Wait for condition to pass
|
|
43
78
|
const start = Date.now();
|
|
44
79
|
while (Date.now() - start < maxDuration) {
|
|
45
|
-
await exports.sleep(pollInterval);
|
|
80
|
+
await (0, exports.sleep)(pollInterval);
|
|
46
81
|
if (condition()) {
|
|
47
82
|
// Success
|
|
48
83
|
return;
|
|
@@ -51,7 +86,6 @@ async function sleepUntil(condition, maxDuration = 2000, pollInterval = 2) {
|
|
|
51
86
|
// maxDuration exceeded
|
|
52
87
|
throw new Error(`Slept for ${Date.now() - start}ms but condition never passed`);
|
|
53
88
|
}
|
|
54
|
-
exports.sleepUntil = sleepUntil;
|
|
55
89
|
/**
|
|
56
90
|
* This is for letting the Node.js event loop advance, e.g. when `setTimeout`
|
|
57
91
|
* has `await` in the chain.
|
|
@@ -60,9 +94,16 @@ exports.sleepUntil = sleepUntil;
|
|
|
60
94
|
*/
|
|
61
95
|
async function aFewRunLoops(count = 5) {
|
|
62
96
|
for (let i = 0; i < count; i++) {
|
|
63
|
-
await exports.sleep(0);
|
|
97
|
+
await (0, exports.sleep)(0);
|
|
64
98
|
}
|
|
65
99
|
}
|
|
100
|
+
const OriginalDate = global.Date;
|
|
101
|
+
/**
|
|
102
|
+
* Returns the real-world current timestamp (unaffected by fake timers).
|
|
103
|
+
*/
|
|
104
|
+
function realNow() {
|
|
105
|
+
return OriginalDate.now();
|
|
106
|
+
}
|
|
66
107
|
/**
|
|
67
108
|
* Sets up fake timers and Date implementation within your Jest test file. You
|
|
68
109
|
* should call this at the top of the file (**not** within a test or an
|
|
@@ -78,91 +119,44 @@ async function aFewRunLoops(count = 5) {
|
|
|
78
119
|
* offset.
|
|
79
120
|
*/
|
|
80
121
|
function setupFakeTimers() {
|
|
81
|
-
jest.useFakeTimers();
|
|
82
|
-
|
|
83
|
-
/** The offset, in milliseconds, to apply to results from `Date.now()` */
|
|
84
|
-
let offset = 0;
|
|
85
|
-
function fakeNow() {
|
|
86
|
-
return OriginalDate.now() + offset;
|
|
87
|
-
}
|
|
122
|
+
beforeEach(() => void jest.useFakeTimers());
|
|
123
|
+
afterEach(() => void jest.useRealTimers());
|
|
88
124
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*/
|
|
92
|
-
const FakeDate = function (...args) {
|
|
93
|
-
if (args.length === 0) {
|
|
94
|
-
// `new Date()` becomes `new Date(fakeNow())`
|
|
95
|
-
return new OriginalDate(fakeNow());
|
|
96
|
-
}
|
|
97
|
-
else if (args.length === 1) {
|
|
98
|
-
// As before
|
|
99
|
-
return new OriginalDate(args[0]);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
// As before
|
|
103
|
-
return new OriginalDate(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
// Copy static methods of Date, overriding Date.now()
|
|
107
|
-
FakeDate.now = () => fakeNow();
|
|
108
|
-
FakeDate.parse = Date.parse;
|
|
109
|
-
FakeDate.UTC = Date.UTC;
|
|
110
|
-
/**
|
|
111
|
-
* Sets the `offset` such that a call to `Date.now()` (or `new Date()`) would
|
|
112
|
-
* return this timestamp if called immediately (but time continues to
|
|
125
|
+
* Sets the fake time such that a call to `Date.now()` (or `new Date()`)
|
|
126
|
+
* would return this timestamp if called immediately (but time continues to
|
|
113
127
|
* progress as expected after this). Also advances the timers by the
|
|
114
|
-
* difference from the previous
|
|
115
|
-
*
|
|
116
|
-
*
|
|
128
|
+
* difference from the previous time, if positive. Setting time backwards is
|
|
129
|
+
* allowed (like setting back the system clock on a computer), but will not
|
|
130
|
+
* advance (or undo the advancement of) any timers.
|
|
117
131
|
*
|
|
118
132
|
* Since advancing the time a few hours might not run all the intermediary
|
|
119
133
|
* code quite right, we actually step it up by a configurable increment
|
|
120
134
|
* (defaults to one minute) at a time. Setting time backwards (no matter how
|
|
121
135
|
* far back) is done all at once.
|
|
122
136
|
*
|
|
123
|
-
* @param
|
|
137
|
+
* @param targetTimestamp - the target timestamp
|
|
124
138
|
* @param increment - the maximum we should advance time by at once in order to step towards `timestamp`
|
|
125
139
|
*/
|
|
126
|
-
async function setTime(
|
|
127
|
-
assert.strictEqual(typeof
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (advancement < 0) {
|
|
131
|
-
offset = finalOffset;
|
|
140
|
+
async function setTime(targetTimestamp, increment = exports.MINUTE) {
|
|
141
|
+
assert.strictEqual(typeof targetTimestamp, "number", `Expected \`setTime\` to be passed a number of milliseconds, instead received '${util.inspect(targetTimestamp)}'`);
|
|
142
|
+
if (targetTimestamp < Date.now()) {
|
|
143
|
+
jest.setSystemTime(targetTimestamp);
|
|
132
144
|
}
|
|
133
145
|
else {
|
|
134
|
-
|
|
135
|
-
while (previousOffset + increment < finalOffset) {
|
|
136
|
-
offset = previousOffset + increment;
|
|
137
|
-
previousOffset = offset;
|
|
146
|
+
while (Date.now() + increment < targetTimestamp) {
|
|
138
147
|
jest.advanceTimersByTime(increment);
|
|
139
148
|
await aFewRunLoops();
|
|
140
149
|
}
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
jest.advanceTimersByTime(finalOffset - previousOffset);
|
|
150
|
+
if (Date.now() < targetTimestamp) {
|
|
151
|
+
jest.advanceTimersByTime(targetTimestamp - Date.now());
|
|
144
152
|
await aFewRunLoops();
|
|
145
153
|
}
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
|
-
beforeEach(() => {
|
|
149
|
-
offset = 0;
|
|
150
|
-
global.Date = FakeDate;
|
|
151
|
-
});
|
|
152
|
-
afterEach(() => {
|
|
153
|
-
global.Date = OriginalDate;
|
|
154
|
-
});
|
|
155
|
-
/**
|
|
156
|
-
* Returns the real-world current timestamp (unaffected by fake timers).
|
|
157
|
-
*/
|
|
158
|
-
function realNow() {
|
|
159
|
-
return OriginalDate.now();
|
|
160
|
-
}
|
|
161
156
|
// In future we may add other methods such as `setTimeWithoutAdvancingTimers`
|
|
162
157
|
// to emulate the system clock changing without real time elapsing.
|
|
163
158
|
return { setTime, realNow };
|
|
164
159
|
}
|
|
165
|
-
exports.setupFakeTimers = setupFakeTimers;
|
|
166
160
|
/** One second in milliseconds */
|
|
167
161
|
exports.SECOND = 1000;
|
|
168
162
|
/** One minute in milliseconds */
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,gCAyBC;AAqCD,0CA8CC;AAjJD,+CAAiC;AACjC,2CAA6B;AAE7B,+EAA+E;AAC/E,MAAM,wBAAwB,GAAG,MAAM,CAAC,UAAU,CAAC;AAEnD;;;;;GAKG;AACI,MAAM,KAAK,GAAG,CACnB,EAAU,EACV,KAAK,GAAG,KAAK,EACgC,EAAE;IAC/C,IAAI,OAAwB,CAAC;IAC7B,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QAC5C,OAAO,GAAG,wBAAwB,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IACH,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC;IACD,O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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jest-time-helpers",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Helpers you can use in tests that relate to the passage of time (i.e. code that involves setTimeout, setInterval, new Date(), Date.now(), etc)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"lint": "yarn prettier:check && eslint --ext .js,.jsx,.ts,.tsx,.graphql .",
|
|
11
11
|
"lint:fix": "eslint --ext .js,.jsx,.ts,.tsx,.graphql . --fix; prettier --ignore-path .eslintignore --write '**/*.{js,jsx,ts,tsx,graphql,md,json}'",
|
|
12
12
|
"prettier:check": "prettier --ignore-path .eslintignore --check '**/*.{js,jsx,ts,tsx,graphql,md,json}'",
|
|
13
|
-
"test": "yarn prepack && jest && depcheck",
|
|
13
|
+
"test": "yarn prepack && jest && depcheck --ignores @types/*,@tsconfig/*",
|
|
14
14
|
"preversion": "grep '^### Pending' RELEASE_NOTES.md && echo \"⚠️ Cannot publish with 'Pending' in RELEASE_NOTES ⚠️\" && exit 1 || true"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
@@ -39,10 +39,12 @@
|
|
|
39
39
|
},
|
|
40
40
|
"homepage": "https://github.com/graphile/jest-time-helpers#readme",
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"jest": ">=22 <
|
|
42
|
+
"jest": ">=22 <31"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@
|
|
45
|
+
"@tsconfig/node20": "^20.1.9",
|
|
46
|
+
"@types/jest": "^30.0.0",
|
|
47
|
+
"@types/node": "^20.0.0",
|
|
46
48
|
"@typescript-eslint/eslint-plugin": "^4.13.0",
|
|
47
49
|
"@typescript-eslint/parser": "^4.13.0",
|
|
48
50
|
"depcheck": "^1.3.1",
|
|
@@ -51,11 +53,11 @@
|
|
|
51
53
|
"eslint-plugin-import": "^2.22.1",
|
|
52
54
|
"eslint-plugin-jest": "^24.1.3",
|
|
53
55
|
"eslint-plugin-simple-import-sort": "^7.0.0",
|
|
54
|
-
"jest": "^
|
|
56
|
+
"jest": "^30.2.0",
|
|
55
57
|
"prettier": "^2.2.1",
|
|
56
|
-
"ts-jest": "^
|
|
58
|
+
"ts-jest": "^29.4.6",
|
|
57
59
|
"typedoc": "^0.20.14",
|
|
58
|
-
"typescript": "^
|
|
60
|
+
"typescript": "^5.9.3"
|
|
59
61
|
},
|
|
60
62
|
"files": [
|
|
61
63
|
"dist"
|