@trackunit/react-vite-test-setup 0.0.48 → 0.0.49-alpha-df0e8d549b3.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/index.cjs.js +7 -73
- package/index.esm.js +3 -68
- package/package.json +1 -1
- package/src/index.d.ts +0 -1
- package/src/configureReliableAsyncUtils.d.ts +0 -31
package/index.cjs.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var react = require('@testing-library/react');
|
|
4
|
-
var vitest = require('vitest');
|
|
5
3
|
require('vitest-canvas-mock');
|
|
6
4
|
var failOnConsole = require('vitest-fail-on-console');
|
|
7
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var vitest = require('vitest');
|
|
8
7
|
require('@testing-library/jest-dom/vitest');
|
|
8
|
+
var react$1 = require('@testing-library/react');
|
|
9
9
|
var util = require('util');
|
|
10
|
-
var react
|
|
10
|
+
var react = require('react');
|
|
11
11
|
var reactI18next = require('react-i18next');
|
|
12
12
|
var polyfill = require('@js-temporal/polyfill');
|
|
13
13
|
var web = require('@react-spring/web');
|
|
@@ -32,66 +32,6 @@ function _interopNamespaceDefault(e) {
|
|
|
32
32
|
|
|
33
33
|
var reactI18next__namespace = /*#__PURE__*/_interopNamespaceDefault(reactI18next);
|
|
34
34
|
|
|
35
|
-
// Tracks the decorators we've installed so repeated `setupReactTestingLibrary()`
|
|
36
|
-
// calls don't stack the decorator on top of itself.
|
|
37
|
-
const reliableAsyncWrappers = new WeakSet();
|
|
38
|
-
/**
|
|
39
|
-
* Makes React Testing Library's async utilities (`waitFor`, `findBy*`,
|
|
40
|
-
* `waitForElementToBeRemoved`) poll in real wall-clock time even though
|
|
41
|
-
* `setupTimeAndLanguage` collapses the global `setTimeout`/`setInterval` to fire
|
|
42
|
-
* at 0ms.
|
|
43
|
-
*
|
|
44
|
-
* Every RTL async utility funnels through the configurable `asyncWrapper`:
|
|
45
|
-
* `@testing-library/react` installs an `act`-based one on import, and
|
|
46
|
-
* `@testing-library/dom`'s `waitFor` — plus `findBy*` and
|
|
47
|
-
* `waitForElementToBeRemoved`, which build on the module-internal `waitFor` —
|
|
48
|
-
* run their poll/timeout loop inside it. We decorate that single seam: for the
|
|
49
|
-
* duration of each async-utility call we temporarily restore the real timers
|
|
50
|
-
* (`globalThis.ORG_setTimeout`/`ORG_setInterval`), then put the collapsed 0ms
|
|
51
|
-
* timers back in a `finally`. Timers stay instant everywhere else, so debounces
|
|
52
|
-
* and animations still resolve immediately and the suite does not slow down.
|
|
53
|
-
*
|
|
54
|
-
* Guards:
|
|
55
|
-
* - Delegates unchanged when fake timers are active (`vi.isFakeTimers()`), so
|
|
56
|
-
* opt-in fake-timer tests keep RTL's fake-timer path.
|
|
57
|
-
* - No-ops the timer swap when `ORG_setTimeout` is absent (projects that don't
|
|
58
|
-
* collapse timers), leaving behaviour unchanged there.
|
|
59
|
-
* - Idempotent: repeated calls don't stack decorators.
|
|
60
|
-
*
|
|
61
|
-
* Must run after RTL's `act` wrapper is installed, i.e. from
|
|
62
|
-
* `setupReactTestingLibrary()`.
|
|
63
|
-
*/
|
|
64
|
-
const configureReliableAsyncUtils = () => {
|
|
65
|
-
const originalAsyncWrapper = react.getConfig().asyncWrapper;
|
|
66
|
-
if (reliableAsyncWrappers.has(originalAsyncWrapper)) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const reliableAsyncWrapper = async (cb) => {
|
|
70
|
-
const orgSetTimeout = globalThis.ORG_setTimeout;
|
|
71
|
-
const orgSetInterval = globalThis.ORG_setInterval;
|
|
72
|
-
// Preserve fake-timer tests, and don't touch projects that never collapsed
|
|
73
|
-
// timers in the first place.
|
|
74
|
-
if (vitest.vi.isFakeTimers() || orgSetTimeout === undefined) {
|
|
75
|
-
return originalAsyncWrapper(cb);
|
|
76
|
-
}
|
|
77
|
-
const collapsedSetTimeout = global.setTimeout;
|
|
78
|
-
const collapsedSetInterval = global.setInterval;
|
|
79
|
-
global.setTimeout = orgSetTimeout;
|
|
80
|
-
if (orgSetInterval !== undefined) {
|
|
81
|
-
global.setInterval = orgSetInterval;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
return await originalAsyncWrapper(cb);
|
|
85
|
-
}
|
|
86
|
-
finally {
|
|
87
|
-
global.setTimeout = collapsedSetTimeout;
|
|
88
|
-
global.setInterval = collapsedSetInterval;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
reliableAsyncWrappers.add(reliableAsyncWrapper);
|
|
92
|
-
react.configure({ asyncWrapper: reliableAsyncWrapper });
|
|
93
|
-
};
|
|
94
|
-
|
|
95
35
|
/**
|
|
96
36
|
* Sets up a mock implementation for HTML Canvas API in testing environments.
|
|
97
37
|
*
|
|
@@ -659,13 +599,13 @@ const setupTranslations = () => {
|
|
|
659
599
|
}
|
|
660
600
|
return Object.keys(reactNodes).map((key, i) => {
|
|
661
601
|
const child = reactNodes[key];
|
|
662
|
-
const isElement = react
|
|
602
|
+
const isElement = react.isValidElement(child);
|
|
663
603
|
if (typeof child === "string") {
|
|
664
604
|
return child;
|
|
665
605
|
}
|
|
666
606
|
if (hasChildren(child)) {
|
|
667
607
|
const inner = renderNodes(getChildren(child));
|
|
668
|
-
return react
|
|
608
|
+
return react.cloneElement(child, { ...child.props, key: i }, inner);
|
|
669
609
|
}
|
|
670
610
|
if (typeof child === "object" && !isElement) {
|
|
671
611
|
return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, "");
|
|
@@ -801,7 +741,7 @@ const installPostMessagePolyfill = () => {
|
|
|
801
741
|
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
802
742
|
*/
|
|
803
743
|
const flushPromisesInAct = (waitTimeInMS = 0) => {
|
|
804
|
-
return react.act(() => {
|
|
744
|
+
return react$1.act(() => {
|
|
805
745
|
return new Promise(resolve => {
|
|
806
746
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
807
747
|
if (global.ORG_setTimeout) {
|
|
@@ -824,7 +764,7 @@ vitest.afterEach(async () => {
|
|
|
824
764
|
// `vi.useRealTimers()` is a no-op when no fake timers are installed.
|
|
825
765
|
vitest.vi.useRealTimers();
|
|
826
766
|
vitest.vi.clearAllMocks();
|
|
827
|
-
react.cleanup();
|
|
767
|
+
react$1.cleanup();
|
|
828
768
|
await flushPromisesInAct();
|
|
829
769
|
});
|
|
830
770
|
const setupResponseForTanstackRouter = () => {
|
|
@@ -1033,11 +973,6 @@ const setupReactTestingLibrary = () => {
|
|
|
1033
973
|
setupResizeObserver();
|
|
1034
974
|
setupIntersectionObserver();
|
|
1035
975
|
installPostMessagePolyfill();
|
|
1036
|
-
// Make RTL's async utilities (waitFor/findBy*/waitForElementToBeRemoved) wait
|
|
1037
|
-
// in real wall-clock time despite the 0ms timer collapse from
|
|
1038
|
-
// `setupTimeAndLanguage`. Runs last so RTL's `act` asyncWrapper is already
|
|
1039
|
-
// installed before we decorate it. See `configureReliableAsyncUtils`.
|
|
1040
|
-
configureReliableAsyncUtils();
|
|
1041
976
|
};
|
|
1042
977
|
|
|
1043
978
|
/**
|
|
@@ -1357,7 +1292,6 @@ const setupTanstackReactRouter = () => {
|
|
|
1357
1292
|
}));
|
|
1358
1293
|
};
|
|
1359
1294
|
|
|
1360
|
-
exports.configureReliableAsyncUtils = configureReliableAsyncUtils;
|
|
1361
1295
|
exports.createMockClickEvent = createMockClickEvent;
|
|
1362
1296
|
exports.createMockMapboxMap = createMockMapboxMap;
|
|
1363
1297
|
exports.setupAllMocks = setupAllMocks;
|
package/index.esm.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { getConfig, configure, cleanup, act } from '@testing-library/react';
|
|
2
|
-
import { vi, beforeEach, afterEach } from 'vitest';
|
|
3
1
|
import 'vitest-canvas-mock';
|
|
4
2
|
import failOnConsole from 'vitest-fail-on-console';
|
|
5
3
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
4
|
+
import { vi, beforeEach, afterEach } from 'vitest';
|
|
6
5
|
import '@testing-library/jest-dom/vitest';
|
|
6
|
+
import { cleanup, act } from '@testing-library/react';
|
|
7
7
|
import { TextEncoder, TextDecoder } from 'util';
|
|
8
8
|
import { isValidElement, cloneElement } from 'react';
|
|
9
9
|
import * as reactI18next from 'react-i18next';
|
|
@@ -11,66 +11,6 @@ import { Temporal } from '@js-temporal/polyfill';
|
|
|
11
11
|
import { Globals } from '@react-spring/web';
|
|
12
12
|
import { WritableStream, TransformStream } from 'web-streams-polyfill';
|
|
13
13
|
|
|
14
|
-
// Tracks the decorators we've installed so repeated `setupReactTestingLibrary()`
|
|
15
|
-
// calls don't stack the decorator on top of itself.
|
|
16
|
-
const reliableAsyncWrappers = new WeakSet();
|
|
17
|
-
/**
|
|
18
|
-
* Makes React Testing Library's async utilities (`waitFor`, `findBy*`,
|
|
19
|
-
* `waitForElementToBeRemoved`) poll in real wall-clock time even though
|
|
20
|
-
* `setupTimeAndLanguage` collapses the global `setTimeout`/`setInterval` to fire
|
|
21
|
-
* at 0ms.
|
|
22
|
-
*
|
|
23
|
-
* Every RTL async utility funnels through the configurable `asyncWrapper`:
|
|
24
|
-
* `@testing-library/react` installs an `act`-based one on import, and
|
|
25
|
-
* `@testing-library/dom`'s `waitFor` — plus `findBy*` and
|
|
26
|
-
* `waitForElementToBeRemoved`, which build on the module-internal `waitFor` —
|
|
27
|
-
* run their poll/timeout loop inside it. We decorate that single seam: for the
|
|
28
|
-
* duration of each async-utility call we temporarily restore the real timers
|
|
29
|
-
* (`globalThis.ORG_setTimeout`/`ORG_setInterval`), then put the collapsed 0ms
|
|
30
|
-
* timers back in a `finally`. Timers stay instant everywhere else, so debounces
|
|
31
|
-
* and animations still resolve immediately and the suite does not slow down.
|
|
32
|
-
*
|
|
33
|
-
* Guards:
|
|
34
|
-
* - Delegates unchanged when fake timers are active (`vi.isFakeTimers()`), so
|
|
35
|
-
* opt-in fake-timer tests keep RTL's fake-timer path.
|
|
36
|
-
* - No-ops the timer swap when `ORG_setTimeout` is absent (projects that don't
|
|
37
|
-
* collapse timers), leaving behaviour unchanged there.
|
|
38
|
-
* - Idempotent: repeated calls don't stack decorators.
|
|
39
|
-
*
|
|
40
|
-
* Must run after RTL's `act` wrapper is installed, i.e. from
|
|
41
|
-
* `setupReactTestingLibrary()`.
|
|
42
|
-
*/
|
|
43
|
-
const configureReliableAsyncUtils = () => {
|
|
44
|
-
const originalAsyncWrapper = getConfig().asyncWrapper;
|
|
45
|
-
if (reliableAsyncWrappers.has(originalAsyncWrapper)) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const reliableAsyncWrapper = async (cb) => {
|
|
49
|
-
const orgSetTimeout = globalThis.ORG_setTimeout;
|
|
50
|
-
const orgSetInterval = globalThis.ORG_setInterval;
|
|
51
|
-
// Preserve fake-timer tests, and don't touch projects that never collapsed
|
|
52
|
-
// timers in the first place.
|
|
53
|
-
if (vi.isFakeTimers() || orgSetTimeout === undefined) {
|
|
54
|
-
return originalAsyncWrapper(cb);
|
|
55
|
-
}
|
|
56
|
-
const collapsedSetTimeout = global.setTimeout;
|
|
57
|
-
const collapsedSetInterval = global.setInterval;
|
|
58
|
-
global.setTimeout = orgSetTimeout;
|
|
59
|
-
if (orgSetInterval !== undefined) {
|
|
60
|
-
global.setInterval = orgSetInterval;
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
return await originalAsyncWrapper(cb);
|
|
64
|
-
}
|
|
65
|
-
finally {
|
|
66
|
-
global.setTimeout = collapsedSetTimeout;
|
|
67
|
-
global.setInterval = collapsedSetInterval;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
reliableAsyncWrappers.add(reliableAsyncWrapper);
|
|
71
|
-
configure({ asyncWrapper: reliableAsyncWrapper });
|
|
72
|
-
};
|
|
73
|
-
|
|
74
14
|
/**
|
|
75
15
|
* Sets up a mock implementation for HTML Canvas API in testing environments.
|
|
76
16
|
*
|
|
@@ -1012,11 +952,6 @@ const setupReactTestingLibrary = () => {
|
|
|
1012
952
|
setupResizeObserver();
|
|
1013
953
|
setupIntersectionObserver();
|
|
1014
954
|
installPostMessagePolyfill();
|
|
1015
|
-
// Make RTL's async utilities (waitFor/findBy*/waitForElementToBeRemoved) wait
|
|
1016
|
-
// in real wall-clock time despite the 0ms timer collapse from
|
|
1017
|
-
// `setupTimeAndLanguage`. Runs last so RTL's `act` asyncWrapper is already
|
|
1018
|
-
// installed before we decorate it. See `configureReliableAsyncUtils`.
|
|
1019
|
-
configureReliableAsyncUtils();
|
|
1020
955
|
};
|
|
1021
956
|
|
|
1022
957
|
/**
|
|
@@ -1336,4 +1271,4 @@ const setupTanstackReactRouter = () => {
|
|
|
1336
1271
|
}));
|
|
1337
1272
|
};
|
|
1338
1273
|
|
|
1339
|
-
export {
|
|
1274
|
+
export { createMockClickEvent, createMockMapboxMap, setupAllMocks, setupBasicMocks, setupCanvasMock, setupDefaultMocks, setupFailOnConsole, setupGoogleMaps, setupHelmetMock, setupIntersectionObserver, setupMapbox, setupMatchMediaMock, setupReactTestingLibrary, setupReactVirtualizedAutoSizer, setupResizeObserver, setupTanstackReactRouter, setupTanstackReactVirtual, setupTimeAndLanguage, setupTimeZone, setupTranslations, setupWebStreams };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-vite-test-setup",
|
|
3
3
|
"description": "Test setup utilities for React applications",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.49-alpha-df0e8d549b3.0",
|
|
5
5
|
"repository": "https://github.com/Trackunit/manager",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
7
7
|
"engines": {
|
package/src/index.d.ts
CHANGED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
declare global {
|
|
2
|
-
var ORG_setTimeout: typeof setTimeout | undefined;
|
|
3
|
-
var ORG_setInterval: typeof setInterval | undefined;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Makes React Testing Library's async utilities (`waitFor`, `findBy*`,
|
|
7
|
-
* `waitForElementToBeRemoved`) poll in real wall-clock time even though
|
|
8
|
-
* `setupTimeAndLanguage` collapses the global `setTimeout`/`setInterval` to fire
|
|
9
|
-
* at 0ms.
|
|
10
|
-
*
|
|
11
|
-
* Every RTL async utility funnels through the configurable `asyncWrapper`:
|
|
12
|
-
* `@testing-library/react` installs an `act`-based one on import, and
|
|
13
|
-
* `@testing-library/dom`'s `waitFor` — plus `findBy*` and
|
|
14
|
-
* `waitForElementToBeRemoved`, which build on the module-internal `waitFor` —
|
|
15
|
-
* run their poll/timeout loop inside it. We decorate that single seam: for the
|
|
16
|
-
* duration of each async-utility call we temporarily restore the real timers
|
|
17
|
-
* (`globalThis.ORG_setTimeout`/`ORG_setInterval`), then put the collapsed 0ms
|
|
18
|
-
* timers back in a `finally`. Timers stay instant everywhere else, so debounces
|
|
19
|
-
* and animations still resolve immediately and the suite does not slow down.
|
|
20
|
-
*
|
|
21
|
-
* Guards:
|
|
22
|
-
* - Delegates unchanged when fake timers are active (`vi.isFakeTimers()`), so
|
|
23
|
-
* opt-in fake-timer tests keep RTL's fake-timer path.
|
|
24
|
-
* - No-ops the timer swap when `ORG_setTimeout` is absent (projects that don't
|
|
25
|
-
* collapse timers), leaving behaviour unchanged there.
|
|
26
|
-
* - Idempotent: repeated calls don't stack decorators.
|
|
27
|
-
*
|
|
28
|
-
* Must run after RTL's `act` wrapper is installed, i.e. from
|
|
29
|
-
* `setupReactTestingLibrary()`.
|
|
30
|
-
*/
|
|
31
|
-
export declare const configureReliableAsyncUtils: () => void;
|