@trackunit/react-vite-test-setup 0.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.
- package/README.md +87 -0
- package/index.cjs.js +1242 -0
- package/index.d.ts +1 -0
- package/index.esm.js +1201 -0
- package/package.json +43 -0
- package/preset.cjs.js +101 -0
- package/preset.d.ts +2 -0
- package/preset.esm.js +77 -0
- package/src/index.d.ts +18 -0
- package/src/preset/vitestPreset.d.ts +48 -0
- package/src/preset.d.ts +3 -0
- package/src/setupAllMocks.d.ts +39 -0
- package/src/setupBasicMocks.d.ts +43 -0
- package/src/setupCanvasMock.d.ts +19 -0
- package/src/setupDefaultMocks.d.ts +49 -0
- package/src/setupFailOnConsole.d.ts +19 -0
- package/src/setupGoogleMaps.d.ts +27 -0
- package/src/setupHelmetMock.d.ts +17 -0
- package/src/setupIntersectionObserver.d.ts +33 -0
- package/src/setupMapbox.d.ts +120 -0
- package/src/setupMatchMediaMock.d.ts +23 -0
- package/src/setupReactTestingLibrary.d.ts +44 -0
- package/src/setupReactVirtualizedAutoSizer.d.ts +18 -0
- package/src/setupResizeObserver.d.ts +19 -0
- package/src/setupTanstackReactRouter.d.ts +18 -0
- package/src/setupTanstackReactVirtualize.d.ts +17 -0
- package/src/setupTimeAndLanguage.d.ts +35 -0
- package/src/setupTranslations.d.ts +17 -0
- package/src/setupWebStreams.d.ts +16 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks the window.matchMedia API for testing environments.
|
|
3
|
+
*
|
|
4
|
+
* This function creates a mock implementation of the window.matchMedia method that
|
|
5
|
+
* is commonly used for responsive design and media queries. The mock always returns
|
|
6
|
+
* a MediaQueryList-like object with standard methods and properties, but with default
|
|
7
|
+
* values (matches set to false).
|
|
8
|
+
*
|
|
9
|
+
* This allows tests of components that use media queries to run without errors in
|
|
10
|
+
* Jest's JSDOM environment, which doesn't implement matchMedia natively.
|
|
11
|
+
*
|
|
12
|
+
* Ideal for testing:
|
|
13
|
+
* - Responsive components
|
|
14
|
+
* - Components that adapt to screen size changes
|
|
15
|
+
* - Components that use CSS media query matching in JavaScript
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // In your jest setup file
|
|
19
|
+
* import { setupMatchMediaMock } from '@trackunit/react-vite-test-setup';
|
|
20
|
+
*
|
|
21
|
+
* setupMatchMediaMock();
|
|
22
|
+
*/
|
|
23
|
+
export declare const setupMatchMediaMock: () => void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import "@testing-library/jest-dom/vitest";
|
|
2
|
+
/**
|
|
3
|
+
* Sets up React Testing Library and Okta authentication mocks for testing.
|
|
4
|
+
*
|
|
5
|
+
* Behaviour:
|
|
6
|
+
* - The top-level `afterEach` further up this file (which clears mocks
|
|
7
|
+
* and runs RTL `cleanup()`) is registered as a side effect of importing
|
|
8
|
+
* this module - that's intentional and existed before this refactor.
|
|
9
|
+
* - Calling `setupReactTestingLibrary()` additionally:
|
|
10
|
+
* 1. Registers the `@okta/okta-react` mock via `vi.doMock` so
|
|
11
|
+
* `useOktaAuth()` returns a fixed authenticated session. Opt-in:
|
|
12
|
+
* the mock is NOT hoisted, so tests that need the real okta
|
|
13
|
+
* hook (or their own mock) are unaffected unless they explicitly
|
|
14
|
+
* call this.
|
|
15
|
+
* 2. Installs the tanstack-router Response polyfill (keeps it
|
|
16
|
+
* opt-in because it mutates `globalThis.Response`);
|
|
17
|
+
* 3. Calls `setupTranslations()` so the
|
|
18
|
+
* `useTranslation`/`useNamespaceTranslation` hooks always
|
|
19
|
+
* return a mocked i18next instance. Under Vitest, the
|
|
20
|
+
* shared jest-preset's `setupFilesAfterEnv` is no longer
|
|
21
|
+
* loaded, so projects that previously got translation
|
|
22
|
+
* setup "for free" otherwise blow up at the first
|
|
23
|
+
* `i18NextInstance.exists is not a function` call. Baking
|
|
24
|
+
* it in here means projects can't forget it - the cost is
|
|
25
|
+
* mocking i18n in tests that don't use it (harmless: the
|
|
26
|
+
* mock only kicks in if the spec imports react-i18next).
|
|
27
|
+
* 4. Installs `setupMatchMediaMock()`, `setupResizeObserver()`
|
|
28
|
+
* and `setupIntersectionObserver()`. Every component test
|
|
29
|
+
* needs these three under jsdom, and missing any of them
|
|
30
|
+
* causes React's render to throw, which `vitest-fail-on-console`
|
|
31
|
+
* then re-raises as "Expected test not to call console.error()".
|
|
32
|
+
* Under jest the workspace preset installed them via
|
|
33
|
+
* `setupFilesAfterEnv`; under vitest only what each project's
|
|
34
|
+
* setup file explicitly imports runs, so most setup files
|
|
35
|
+
* called `setupReactTestingLibrary()` plus `setupFailOnConsole()`
|
|
36
|
+
* but forgot the basic-DOM triplet. Folding the triplet in
|
|
37
|
+
* here means "set up RTL" reads naturally as "set up
|
|
38
|
+
* everything you need to render a component".
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* import { setupReactTestingLibrary } from '@trackunit/react-vite-test-setup';
|
|
42
|
+
* setupReactTestingLibrary();
|
|
43
|
+
*/
|
|
44
|
+
export declare const setupReactTestingLibrary: () => void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks the `react-virtualized-auto-sizer` component for testing
|
|
3
|
+
* environments.
|
|
4
|
+
*
|
|
5
|
+
* Provides fixed dimensions (600x600) so tests don't need real DOM
|
|
6
|
+
* measurements. Especially useful for components using virtualized lists or
|
|
7
|
+
* grids.
|
|
8
|
+
*
|
|
9
|
+
* Opt-in: the mock is registered via `vi.doMock` so it is
|
|
10
|
+
* NOT hoisted and only takes effect once `setupReactVirtualizedAutoSizer()`
|
|
11
|
+
* is called from a test setup file. Tests that need the real component (or
|
|
12
|
+
* their own mock) are unaffected unless they explicitly call this.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { setupReactVirtualizedAutoSizer } from '@trackunit/react-vite-test-setup';
|
|
16
|
+
* setupReactVirtualizedAutoSizer();
|
|
17
|
+
*/
|
|
18
|
+
export declare const setupReactVirtualizedAutoSizer: () => void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock the ResizeObserver to be able to test components that use a resize
|
|
3
|
+
* observer (e.g. useMeasure, useContainerBreakpoints, and components that
|
|
4
|
+
* use them like BaseInput). Recommended for all React libs.
|
|
5
|
+
*
|
|
6
|
+
* Implementation note: under Vitest the mock was a single
|
|
7
|
+
* `vi.fn().mockImplementation(...)` so callers could:
|
|
8
|
+
* 1. `new global.ResizeObserver(cb)` from the SUT (the implementation
|
|
9
|
+
* function is callable with `new` because it's a `function` expression
|
|
10
|
+
* which has a working `[[Construct]]` slot), AND
|
|
11
|
+
* 2. `(global.ResizeObserver as Mock).mockImplementation(...)` from the
|
|
12
|
+
* spec to capture the callback / inspect constructor arguments.
|
|
13
|
+
*
|
|
14
|
+
* Vitest 4's `vi.fn()` is also constructable when the impl is a `function`
|
|
15
|
+
* expression (arrow functions are *not* constructable), so we keep the
|
|
16
|
+
* same shape - this preserves both call sites that the migration would
|
|
17
|
+
* otherwise have to rewrite.
|
|
18
|
+
*/
|
|
19
|
+
export declare const setupResizeObserver: () => void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks the `@tanstack/react-router` library for testing environments.
|
|
3
|
+
*
|
|
4
|
+
* Replaces `useNavigate` with a no-op so components that call the
|
|
5
|
+
* navigation function during a test do not throw outside a router context.
|
|
6
|
+
*
|
|
7
|
+
* Opt-in: the mock is registered via `vi.doMock` so it is
|
|
8
|
+
* NOT hoisted and only takes effect once `setupTanstackReactRouter()` is
|
|
9
|
+
* called from a test setup file. Tests that need the real `useNavigate`
|
|
10
|
+
* (or their own mock) are unaffected unless they explicitly call this.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // In your vitest setup file
|
|
14
|
+
* import { setupTanstackReactRouter } from '@trackunit/react-vite-test-setup';
|
|
15
|
+
*
|
|
16
|
+
* setupTanstackReactRouter();
|
|
17
|
+
*/
|
|
18
|
+
export declare const setupTanstackReactRouter: () => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks the `@tanstack/react-virtual` library for testing environments.
|
|
3
|
+
*
|
|
4
|
+
* Replaces `useVirtualizer` with a deterministic implementation that
|
|
5
|
+
* returns every item (no real DOM virtualisation), so tests can assert on
|
|
6
|
+
* the full rendered list without worrying about scroll-driven windowing.
|
|
7
|
+
*
|
|
8
|
+
* Opt-in: the mock is registered via `vi.doMock` so it is
|
|
9
|
+
* NOT hoisted and only takes effect once `setupTanstackReactVirtual()` is
|
|
10
|
+
* called from a test setup file. Tests that need the real virtualizer (or
|
|
11
|
+
* their own mock) are unaffected unless they explicitly call this.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { setupTanstackReactVirtual } from '@trackunit/react-vite-test-setup';
|
|
15
|
+
* setupTanstackReactVirtual();
|
|
16
|
+
*/
|
|
17
|
+
export declare const setupTanstackReactVirtual: () => void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks the Temporal API to use a fixed time zone for testing.
|
|
3
|
+
*
|
|
4
|
+
* This function specifically mocks the Temporal.Now.timeZoneId method from the
|
|
5
|
+
* Temporal library to always return "Europe/Copenhagen", ensuring consistent
|
|
6
|
+
* time zone-dependent behavior in tests regardless of where they run.
|
|
7
|
+
*
|
|
8
|
+
* Note: the host process timezone is also pinned to UTC at module load via
|
|
9
|
+
* `process.env.TZ` above, so non-Temporal Date arithmetic is deterministic.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // In your vitest setup file
|
|
13
|
+
* import { setupTimeZone } from '@trackunit/react-vite-test-setup';
|
|
14
|
+
*
|
|
15
|
+
* setupTimeZone();
|
|
16
|
+
*/
|
|
17
|
+
export declare const setupTimeZone: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* Sets up time, animation, and language-related mocks for testing environments.
|
|
20
|
+
*
|
|
21
|
+
* This function configures multiple aspects of the testing environment:
|
|
22
|
+
* 1. Sets a fixed time zone using setupTimeZone()
|
|
23
|
+
* 2. Disables React Spring animations for faster, deterministic tests
|
|
24
|
+
* 3. Overrides setTimeout and setInterval to execute immediately (0ms delay)
|
|
25
|
+
*
|
|
26
|
+
* These changes make tests faster and more predictable by eliminating real-time
|
|
27
|
+
* delays, animations, and time zone dependencies that could cause flaky tests.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // In your jest setup file
|
|
31
|
+
* import { setupTimeAndLanguage } from '@trackunit/react-vite-test-setup';
|
|
32
|
+
*
|
|
33
|
+
* setupTimeAndLanguage();
|
|
34
|
+
*/
|
|
35
|
+
export declare const setupTimeAndLanguage: () => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets up internationalization and translation mocks for testing environments.
|
|
3
|
+
*
|
|
4
|
+
* Mock translations for react-i18next and @trackunit/i18n-library-translation. It creates
|
|
5
|
+
* simple mock implementations that return the key string as the translation, which allows
|
|
6
|
+
* for testing internationalized components without the complexity of actual translations.
|
|
7
|
+
*
|
|
8
|
+
* The mocks support common translation components and hooks like Trans, useTranslation,
|
|
9
|
+
* NamespaceTrans, and useNamespaceTranslation.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // In your jest setup file
|
|
13
|
+
* import { setupTranslations } from '@trackunit/react-vite-test-setup';
|
|
14
|
+
*
|
|
15
|
+
* setupTranslations();
|
|
16
|
+
*/
|
|
17
|
+
export declare const setupTranslations: () => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets up Web Streams API polyfills for testing environments.
|
|
3
|
+
*
|
|
4
|
+
* Web Streams API polyfills from web-streams-polyfill. These polyfills provide implementations
|
|
5
|
+
* of modern streaming APIs that may not be available in the Jest/JSDOM testing environment.
|
|
6
|
+
*
|
|
7
|
+
* The setup enables testing of components and utilities that rely on the Web Streams API,
|
|
8
|
+
* such as those that process streaming data or implement custom stream transformations.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // In your jest setup file
|
|
12
|
+
* import { setupWebStreams } from '@trackunit/react-vite-test-setup';
|
|
13
|
+
*
|
|
14
|
+
* setupWebStreams();
|
|
15
|
+
*/
|
|
16
|
+
export declare const setupWebStreams: () => void;
|