@unrift/core 0.3.0 → 0.4.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/bin/unrift.mjs +2 -1
- package/dist/esm/cli.js +5 -0
- package/dist/esm/expect.d.ts +12 -0
- package/dist/esm/expect.d.ts.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/matchers.d.ts.map +1 -1
- package/dist/esm/matchers.js +161 -1
- package/dist/esm/mock/auto.d.ts +27 -0
- package/dist/esm/mock/auto.d.ts.map +1 -0
- package/dist/esm/mock/auto.js +108 -0
- package/dist/esm/mock/fetch.d.ts +47 -0
- package/dist/esm/mock/fetch.d.ts.map +1 -0
- package/dist/esm/mock/fetch.js +149 -0
- package/dist/esm/mock/fs.d.ts +54 -0
- package/dist/esm/mock/fs.d.ts.map +1 -0
- package/dist/esm/mock/fs.js +125 -0
- package/dist/esm/mock/index.d.ts +70 -0
- package/dist/esm/mock/index.d.ts.map +1 -0
- package/dist/esm/mock/index.js +70 -0
- package/dist/esm/mock/module-loader.d.ts +66 -0
- package/dist/esm/mock/module-loader.d.ts.map +1 -0
- package/dist/esm/mock/module-loader.js +123 -0
- package/dist/esm/mock/module.d.ts +99 -0
- package/dist/esm/mock/module.d.ts.map +1 -0
- package/dist/esm/mock/module.js +198 -0
- package/dist/esm/mock/register-loader.d.ts +17 -0
- package/dist/esm/mock/register-loader.d.ts.map +1 -0
- package/dist/esm/mock/register-loader.js +45 -0
- package/dist/esm/mock/spy.d.ts +73 -0
- package/dist/esm/mock/spy.d.ts.map +1 -0
- package/dist/esm/mock/spy.js +307 -0
- package/dist/esm/mock/stub.d.ts +26 -0
- package/dist/esm/mock/stub.d.ts.map +1 -0
- package/dist/esm/mock/stub.js +82 -0
- package/dist/esm/mock/timers.d.ts +51 -0
- package/dist/esm/mock/timers.d.ts.map +1 -0
- package/dist/esm/mock/timers.js +581 -0
- package/dist/esm/runner.d.ts.map +1 -1
- package/dist/esm/runner.js +13 -8
- package/dist/esm/test.d.ts.map +1 -1
- package/dist/esm/test.js +12 -5
- package/dist/esm/utils/compareEngine.js +3 -3
- package/dist/esm/utils/discoverTestFiles.js +1 -1
- package/dist/esm/utils/helpers.d.ts +10 -0
- package/dist/esm/utils/helpers.d.ts.map +1 -1
- package/dist/esm/utils/helpers.js +13 -0
- package/dist/esm/utils/toThrow.d.ts.map +1 -1
- package/dist/esm/utils/toThrow.js +2 -1
- package/package.json +4 -3
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mock.stub` - replace any property on any object (function or value),
|
|
3
|
+
* with restoration tracked centrally so `mock.restoreAll()` can undo it.
|
|
4
|
+
*
|
|
5
|
+
* Use `spyOn` when you want a wrapped function spy. Use `stub` when you
|
|
6
|
+
* want to swap in a fixed value or non-function replacement.
|
|
7
|
+
*/
|
|
8
|
+
import { activeRestorers } from "./spy.js";
|
|
9
|
+
/**
|
|
10
|
+
* Tracks live stubs per target so a repeat `stub()` on the same (target, key)
|
|
11
|
+
* updates the value in place and returns the ORIGINAL restore. Without this,
|
|
12
|
+
* the second stub would capture the first stub's value as its "original" and
|
|
13
|
+
* restoring would leave the first stub in place instead of the real value.
|
|
14
|
+
*/
|
|
15
|
+
const activeStubs = new WeakMap();
|
|
16
|
+
/**
|
|
17
|
+
* Replace `target[key]` with `value`. Returns a function that restores
|
|
18
|
+
* the original property descriptor (or removes the key entirely if it
|
|
19
|
+
* didn't exist before).
|
|
20
|
+
*
|
|
21
|
+
* Stubbing the same property again before restoring updates the value but
|
|
22
|
+
* keeps the first stub's original - so restore always yields the real value,
|
|
23
|
+
* no matter how many times the property was re-stubbed.
|
|
24
|
+
*
|
|
25
|
+
* The restorer is also registered with the global `mock.restoreAll()`
|
|
26
|
+
* pool, so test cleanup can reset every stub at once.
|
|
27
|
+
*/
|
|
28
|
+
export function stub(target, key, value) {
|
|
29
|
+
// Repeat stub on a live-stubbed key: swap the value, reuse the original
|
|
30
|
+
// descriptor captured by the first stub.
|
|
31
|
+
const existing = activeStubs.get(target)?.get(key);
|
|
32
|
+
if (existing) {
|
|
33
|
+
Object.defineProperty(target, key, {
|
|
34
|
+
value,
|
|
35
|
+
writable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
enumerable: Object.getOwnPropertyDescriptor(target, key)?.enumerable,
|
|
38
|
+
});
|
|
39
|
+
return existing;
|
|
40
|
+
}
|
|
41
|
+
const original = Object.getOwnPropertyDescriptor(target, key);
|
|
42
|
+
const hadOwn = original !== undefined;
|
|
43
|
+
if (original && original.configurable === false) {
|
|
44
|
+
throw new Error(`mock.stub() cannot replace non-configurable property "${String(key)}"`);
|
|
45
|
+
}
|
|
46
|
+
Object.defineProperty(target, key, {
|
|
47
|
+
value,
|
|
48
|
+
writable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
enumerable: original?.enumerable ?? true,
|
|
51
|
+
});
|
|
52
|
+
let restored = false;
|
|
53
|
+
const restore = () => {
|
|
54
|
+
if (restored)
|
|
55
|
+
return;
|
|
56
|
+
restored = true;
|
|
57
|
+
if (hadOwn && original) {
|
|
58
|
+
Object.defineProperty(target, key, original);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Property didn't exist on this object before, remove it to restore original behavior, including prototype inheritance.
|
|
62
|
+
delete target[key];
|
|
63
|
+
}
|
|
64
|
+
activeStubs.get(target)?.delete(key);
|
|
65
|
+
activeRestorers.delete(restore);
|
|
66
|
+
};
|
|
67
|
+
let byKey = activeStubs.get(target);
|
|
68
|
+
if (!byKey) {
|
|
69
|
+
byKey = new Map();
|
|
70
|
+
activeStubs.set(target, byKey);
|
|
71
|
+
}
|
|
72
|
+
byKey.set(key, restore);
|
|
73
|
+
activeRestorers.add(restore);
|
|
74
|
+
return restore;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Convenience wrapper: `mock.stub(globalThis, key, value)`. Useful for
|
|
78
|
+
* swapping `fetch`, `crypto`, `console`, etc. during a test.
|
|
79
|
+
*/
|
|
80
|
+
export function mockGlobal(key, value) {
|
|
81
|
+
return stub(globalThis, key, value);
|
|
82
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fake clock - controllable replacements for `setTimeout`, `setInterval`,
|
|
3
|
+
* `setImmediate`, `process.nextTick`, `queueMicrotask`, `Date`, `Date.now`,
|
|
4
|
+
* and `performance.now`.
|
|
5
|
+
*
|
|
6
|
+
* Tests opt in via `mock.useFakeTimers()` and drain queued tasks deterministically
|
|
7
|
+
* with `advanceTimersByTime(ms)` / `runAllTimers()` / `runOnlyPendingTimers()`.
|
|
8
|
+
*
|
|
9
|
+
* Restoration:
|
|
10
|
+
* - `mock.useRealTimers()` reverts every patched global to its original
|
|
11
|
+
* - `mock.restoreAll()` does the same as part of bulk cleanup
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Names of host APIs that `useFakeTimers` can patch. `setTimeout` implicitly
|
|
15
|
+
* pairs with `clearTimeout`, `setInterval` with `clearInterval`, and
|
|
16
|
+
* `setImmediate` with `clearImmediate` - you can't fake one half of a pair
|
|
17
|
+
* without breaking the other.
|
|
18
|
+
*/
|
|
19
|
+
export type FakeableApi = "setTimeout" | "setInterval" | "setImmediate" | "queueMicrotask" | "process.nextTick" | "Date" | "performance";
|
|
20
|
+
export interface UseFakeTimersOptions {
|
|
21
|
+
now?: Date | string | number;
|
|
22
|
+
/**
|
|
23
|
+
* Opt-in: only the listed APIs are faked; everything else stays real.
|
|
24
|
+
* Mutually exclusive with `doNotFake`.
|
|
25
|
+
*/
|
|
26
|
+
toFake?: readonly FakeableApi[];
|
|
27
|
+
/**
|
|
28
|
+
* Opt-out: everything is faked except the listed APIs.
|
|
29
|
+
* Mutually exclusive with `toFake`.
|
|
30
|
+
*/
|
|
31
|
+
doNotFake?: readonly FakeableApi[];
|
|
32
|
+
}
|
|
33
|
+
export declare function useFakeTimers(options?: UseFakeTimersOptions): void;
|
|
34
|
+
export declare function useRealTimers(): void;
|
|
35
|
+
export declare function advanceTimersByTime(ms: number): void;
|
|
36
|
+
export declare function runAllTimers(): void;
|
|
37
|
+
export declare function runOnlyPendingTimers(): void;
|
|
38
|
+
/**
|
|
39
|
+
* Like `advanceTimersByTime`, but awaits Promise return values from
|
|
40
|
+
* timer callbacks and yields to microtasks between pumps. Use this when
|
|
41
|
+
* the code under test does `await` work inside a `setTimeout` callback.
|
|
42
|
+
*/
|
|
43
|
+
export declare function advanceTimersByTimeAsync(ms: number): Promise<void>;
|
|
44
|
+
/** Async sibling of `runAllTimers`. */
|
|
45
|
+
export declare function runAllTimersAsync(): Promise<void>;
|
|
46
|
+
/** Async sibling of `runOnlyPendingTimers`. */
|
|
47
|
+
export declare function runOnlyPendingTimersAsync(): Promise<void>;
|
|
48
|
+
export declare function getTimerCount(): number;
|
|
49
|
+
export declare function setSystemTime(value: Date | string | number): void;
|
|
50
|
+
export declare function getRealSystemTime(): number;
|
|
51
|
+
//# sourceMappingURL=timers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timers.d.ts","sourceRoot":"","sources":["../../../src/mock/timers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA2PH;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GACnB,YAAY,GACZ,aAAa,GACb,cAAc,GACd,gBAAgB,GAChB,kBAAkB,GAClB,MAAM,GACN,aAAa,CAAC;AAYlB,MAAM,WAAW,oBAAoB;IAEnC,GAAG,CAAC,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;IAC7B;;;OAGG;IACH,MAAM,CAAC,EAAE,SAAS,WAAW,EAAE,CAAC;IAChC;;;OAGG;IACH,SAAS,CAAC,EAAE,SAAS,WAAW,EAAE,CAAC;CACpC;AAmGD,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,GAAG,IAAI,CA4GtE;AAED,wBAAgB,aAAa,IAAI,IAAI,CA4CpC;AAED,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAGpD;AAED,wBAAgB,YAAY,IAAI,IAAI,CAcnC;AAED,wBAAgB,oBAAoB,IAAI,IAAI,CAqB3C;AA4CD;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAiBxE;AAED,uCAAuC;AACvC,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC,CAcvD;AAED,+CAA+C;AAC/C,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,IAAI,CAAC,CAkB/D;AAED,wBAAgB,aAAa,IAAI,MAAM,CAKtC;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAGjE;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAE1C"}
|