vitest-native 0.3.0 → 0.4.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/CHANGELOG.md +88 -0
- package/README.md +254 -11
- package/dist/chunk-CZWwpsFl.cjs +43 -0
- package/dist/{index-CJqS-y_M.d.cts → index-CJIhukN_.d.mts} +58 -4
- package/dist/{index-h4vpIkQO.d.mts → index-DEB7OFaJ.d.cts} +60 -2
- package/dist/index.cjs +252 -21
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +248 -18
- package/dist/jest-compat/interop.mjs +30 -0
- package/dist/jest-compat/jest-globals.d.mts +3 -0
- package/dist/jest-compat/jest-globals.mjs +9 -0
- package/dist/jest-compat/noop.d.mts +3 -0
- package/dist/jest-compat/noop.mjs +7 -0
- package/dist/jest-compat/setup.d.mts +3 -0
- package/dist/jest-compat/setup.mjs +41 -0
- package/dist/jest-compat.cjs +109 -0
- package/dist/jest-compat.d.cts +38 -0
- package/dist/jest-compat.d.mts +38 -0
- package/dist/jest-compat.mjs +104 -0
- package/dist/matchers.cjs +69 -0
- package/dist/matchers.d.cts +51 -0
- package/dist/matchers.d.mts +51 -0
- package/dist/matchers.mjs +66 -0
- package/dist/native/boundary.mjs +225 -0
- package/dist/native/globals.mjs +112 -0
- package/dist/native/hooks.mjs +85 -0
- package/dist/native/loader.mjs +123 -0
- package/dist/native/match.mjs +9 -0
- package/dist/native/reset.mjs +170 -0
- package/dist/native/resolve.mjs +22 -0
- package/dist/native/runner.mjs +18 -0
- package/dist/native/setup.mjs +134 -0
- package/dist/native/transform.mjs +79 -0
- package/dist/native/worker.mjs +96 -0
- package/dist/pool-B4mWN3N5.cjs +60 -0
- package/dist/pool-oPTZiSXU.mjs +58 -0
- package/dist/{preset-map-DxU6UgYS.cjs → preset-map-C3jxnkiX.cjs} +5 -1
- package/dist/{preset-map-BTpoNH3G.mjs → preset-map-OJm5IWuL.mjs} +5 -1
- package/dist/{presets-B-spxZz7.cjs → presets-2I68Ipyl.cjs} +437 -41
- package/dist/{presets-DvrcfEPz.mjs → presets-DOGXQzYT.mjs} +401 -6
- package/dist/presets.cjs +5 -1
- package/dist/presets.d.cts +2 -2
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/setup.cjs +263 -115
- package/dist/setup.mjs +259 -112
- package/docs/migrating-from-jest.md +123 -0
- package/docs/migrating-from-vitest-react-native.md +109 -0
- package/package.json +65 -9
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
//#region src/jest-compat/transform.ts
|
|
3
|
+
const HOISTABLE = new Set([
|
|
4
|
+
"mock",
|
|
5
|
+
"unmock",
|
|
6
|
+
"doMock",
|
|
7
|
+
"doUnmock"
|
|
8
|
+
]);
|
|
9
|
+
const WITH_FACTORY = new Set(["mock", "doMock"]);
|
|
10
|
+
const TRANSFORMABLE = /\.(?:[cm]?[jt]sx?)$/;
|
|
11
|
+
const HAS_JEST_MOCK = /\bjest\s*\.\s*(?:mock|unmock|doMock|doUnmock)\s*\(/;
|
|
12
|
+
/** Visit every node in an ESTree AST (depth-first), calling `fn` on each. */
|
|
13
|
+
function walk(node, fn) {
|
|
14
|
+
if (!node || typeof node.type !== "string") return;
|
|
15
|
+
fn(node);
|
|
16
|
+
for (const key in node) {
|
|
17
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc") continue;
|
|
18
|
+
const child = node[key];
|
|
19
|
+
if (Array.isArray(child)) {
|
|
20
|
+
for (const c of child) if (c && typeof c.type === "string") walk(c, fn);
|
|
21
|
+
} else if (child && typeof child.type === "string") walk(child, fn);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Vite plugin that adapts a Jest suite's `jest.mock(...)` calls for Vitest:
|
|
26
|
+
*
|
|
27
|
+
* 1. **Hoisting** — rewrites the `jest` object of `jest.mock`/`unmock`/`doMock`/
|
|
28
|
+
* `doUnmock` to `vi`, so Vitest's hoister (which only recognises `vi`/`vitest`)
|
|
29
|
+
* lifts them above the imports. Without this a top-level `jest.mock(...)` runs
|
|
30
|
+
* after imports and silently doesn't apply.
|
|
31
|
+
*
|
|
32
|
+
* 2. **CJS interop** — wraps each `jest.mock`/`doMock` factory so its return value
|
|
33
|
+
* passes through Jest's `_interopRequireDefault` semantics (see interop.mjs).
|
|
34
|
+
* Jest treats the factory return as `module.exports`; Vitest treats it as an ES
|
|
35
|
+
* namespace. This bridges the two common Jest shapes that otherwise break:
|
|
36
|
+
* jest.mock('m', () => Component) // Vitest: "not returning an object"
|
|
37
|
+
* jest.mock('m', () => ({ a, b })) // Vitest: default import is undefined
|
|
38
|
+
* A factory already returning an ES shape (`__esModule`/explicit `default`) is
|
|
39
|
+
* passed through unchanged.
|
|
40
|
+
*
|
|
41
|
+
* Opt-in: add after `reactNative()`; pair with `jestCompatSetup` + `globals: true`.
|
|
42
|
+
*/
|
|
43
|
+
function jestMockTransform() {
|
|
44
|
+
return {
|
|
45
|
+
name: "vitest-native:jest-mock-hoist",
|
|
46
|
+
transform(code, id) {
|
|
47
|
+
if (id.includes("/node_modules/")) return null;
|
|
48
|
+
const file = id.split("?")[0];
|
|
49
|
+
if (!TRANSFORMABLE.test(file)) return null;
|
|
50
|
+
if (!HAS_JEST_MOCK.test(code)) return null;
|
|
51
|
+
let ast;
|
|
52
|
+
try {
|
|
53
|
+
ast = this.parse(code);
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const s = new MagicString(code);
|
|
58
|
+
let changed = false;
|
|
59
|
+
walk(ast, (node) => {
|
|
60
|
+
if (node.type !== "CallExpression") return;
|
|
61
|
+
const callee = node.callee;
|
|
62
|
+
if (!callee || callee.type !== "MemberExpression" || callee.computed) return;
|
|
63
|
+
const obj = callee.object;
|
|
64
|
+
const prop = callee.property;
|
|
65
|
+
if (!obj || obj.type !== "Identifier" || obj.name !== "jest") return;
|
|
66
|
+
if (!prop || prop.type !== "Identifier" || !HOISTABLE.has(prop.name)) return;
|
|
67
|
+
s.overwrite(obj.start, obj.end, "vi");
|
|
68
|
+
changed = true;
|
|
69
|
+
if (WITH_FACTORY.has(prop.name) && node.arguments.length >= 2) {
|
|
70
|
+
const factory = node.arguments[1];
|
|
71
|
+
if (factory.type === "ArrowFunctionExpression" || factory.type === "FunctionExpression") {
|
|
72
|
+
s.appendLeft(factory.start, "() => globalThis.__vnInteropMock((");
|
|
73
|
+
s.appendRight(factory.end, ")())");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
if (!changed) return null;
|
|
78
|
+
return {
|
|
79
|
+
code: s.toString(),
|
|
80
|
+
map: s.generateMap({ hires: true })
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/jest-compat/index.ts
|
|
87
|
+
/** Setup-file specifier: gives the suite a `jest` global backed by Vitest's `vi`. */
|
|
88
|
+
const jestCompatSetup = "vitest-native/jest-compat/setup";
|
|
89
|
+
/**
|
|
90
|
+
* `resolve.alias` entries that redirect Jest-only modules to Vitest-backed shims:
|
|
91
|
+
* - `@jest/globals` → a shim re-exporting Vitest globals (unblocks RNTL < 12)
|
|
92
|
+
* - `@testing-library/jest-native/extend-expect` → a no-op (matchers are already
|
|
93
|
+
* registered by vitest-native's setup)
|
|
94
|
+
*
|
|
95
|
+
* Spread into your config's `resolve.alias`.
|
|
96
|
+
*/
|
|
97
|
+
function jestCompatAliases() {
|
|
98
|
+
return {
|
|
99
|
+
"@jest/globals": "vitest-native/jest-compat/jest-globals",
|
|
100
|
+
"@testing-library/jest-native/extend-expect": "vitest-native/jest-compat/extend-expect-noop"
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
export { jestCompatAliases, jestCompatSetup, jestMockTransform };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/matchers/animated.ts
|
|
3
|
+
/** Flatten a React Native `style` prop (object, array, or nested arrays). */
|
|
4
|
+
function flattenStyle(style) {
|
|
5
|
+
if (!style) return {};
|
|
6
|
+
if (Array.isArray(style)) return style.reduce((acc, entry) => Object.assign(acc, flattenStyle(entry)), {});
|
|
7
|
+
if (typeof style === "object") return style;
|
|
8
|
+
return {};
|
|
9
|
+
}
|
|
10
|
+
/** Narrow an unknown value to something with a `props` bag. */
|
|
11
|
+
function getProps(received) {
|
|
12
|
+
if (received && typeof received === "object" && "props" in received) {
|
|
13
|
+
const props = received.props;
|
|
14
|
+
if (props && typeof props === "object") return props;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
function notAnElementResult(ctx, matcherName, received) {
|
|
19
|
+
return {
|
|
20
|
+
pass: false,
|
|
21
|
+
message: () => `${ctx.utils.matcherHint(matcherName, "element", "expected")}\n\nExpected a rendered element with a \`props\` object, but received:\n ${ctx.utils.printReceived(received)}`
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Pick only the keys present in `expected` from `actual`, for readable diffs. */
|
|
25
|
+
function subset(actual, expected) {
|
|
26
|
+
const result = {};
|
|
27
|
+
for (const key of Object.keys(expected)) result[key] = actual[key];
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
function toHaveAnimatedStyle(received, expectedStyle, config = {}) {
|
|
31
|
+
const props = getProps(received);
|
|
32
|
+
if (!props) return notAnElementResult(this, "toHaveAnimatedStyle", received);
|
|
33
|
+
const currentStyle = flattenStyle(props.style);
|
|
34
|
+
const compared = config.shouldMatchAllProps ? currentStyle : subset(currentStyle, expectedStyle);
|
|
35
|
+
const pass = this.equals(compared, expectedStyle);
|
|
36
|
+
return {
|
|
37
|
+
pass,
|
|
38
|
+
message: () => {
|
|
39
|
+
const hint = this.utils.matcherHint("toHaveAnimatedStyle", "element", "expectedStyle", { isNot: this.isNot });
|
|
40
|
+
if (pass) return `${hint}\n\nExpected element not to have animated style:\n ${this.utils.printExpected(expectedStyle)}\nReceived:\n ${this.utils.printReceived(compared)}`;
|
|
41
|
+
const diff = this.utils.diff?.(expectedStyle, compared);
|
|
42
|
+
return diff ? `${hint}\n\n${diff}` : `${hint}\n\nExpected: ${this.utils.printExpected(expectedStyle)}\nReceived: ${this.utils.printReceived(compared)}`;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function toHaveAnimatedProps(received, expectedProps) {
|
|
47
|
+
const props = getProps(received);
|
|
48
|
+
if (!props) return notAnElementResult(this, "toHaveAnimatedProps", received);
|
|
49
|
+
const compared = subset(props.animatedProps && typeof props.animatedProps === "object" ? props.animatedProps : props, expectedProps);
|
|
50
|
+
const pass = this.equals(compared, expectedProps);
|
|
51
|
+
return {
|
|
52
|
+
pass,
|
|
53
|
+
message: () => {
|
|
54
|
+
const hint = this.utils.matcherHint("toHaveAnimatedProps", "element", "expectedProps", { isNot: this.isNot });
|
|
55
|
+
if (pass) return `${hint}\n\nExpected element not to have animated props:\n ${this.utils.printExpected(expectedProps)}\nReceived:\n ${this.utils.printReceived(compared)}`;
|
|
56
|
+
const diff = this.utils.diff?.(expectedProps, compared);
|
|
57
|
+
return diff ? `${hint}\n\n${diff}` : `${hint}\n\nExpected: ${this.utils.printExpected(expectedProps)}\nReceived: ${this.utils.printReceived(compared)}`;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** All reanimated-compatible matchers, keyed by name for `expect.extend`. */
|
|
62
|
+
const animatedMatchers = {
|
|
63
|
+
toHaveAnimatedStyle,
|
|
64
|
+
toHaveAnimatedProps
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
exports.animatedMatchers = animatedMatchers;
|
|
68
|
+
exports.toHaveAnimatedProps = toHaveAnimatedProps;
|
|
69
|
+
exports.toHaveAnimatedStyle = toHaveAnimatedStyle;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/matchers/animated.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reanimated-compatible matchers: `toHaveAnimatedStyle` and `toHaveAnimatedProps`.
|
|
4
|
+
*
|
|
5
|
+
* react-native-reanimated ships these matchers via its Jest setup
|
|
6
|
+
* (`setUpTests()`), so they are unavailable under Vitest by default — calling
|
|
7
|
+
* `expect(node).toHaveAnimatedStyle(...)` throws `Invalid Chai property`.
|
|
8
|
+
*
|
|
9
|
+
* vitest-native's reanimated preset resolves animated hooks synchronously:
|
|
10
|
+
* `useAnimatedStyle(updater)` returns `updater()` and the result is passed
|
|
11
|
+
* through as the element's `style` prop (and `animatedProps` is passed through
|
|
12
|
+
* verbatim). These matchers therefore read straight from the rendered element's
|
|
13
|
+
* props, flattening style arrays the same way React Native does.
|
|
14
|
+
*/
|
|
15
|
+
interface MatcherContext {
|
|
16
|
+
isNot: boolean;
|
|
17
|
+
equals: (a: unknown, b: unknown) => boolean;
|
|
18
|
+
utils: {
|
|
19
|
+
matcherHint: (name: string, received?: string, expected?: string, options?: unknown) => string;
|
|
20
|
+
printExpected: (value: unknown) => string;
|
|
21
|
+
printReceived: (value: unknown) => string;
|
|
22
|
+
diff?: (a: unknown, b: unknown) => string | null;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
interface MatcherResult {
|
|
26
|
+
pass: boolean;
|
|
27
|
+
message: () => string;
|
|
28
|
+
}
|
|
29
|
+
interface AnimatedStyleConfig {
|
|
30
|
+
/** When true, every key of the current style must match `expectedStyle` exactly. */
|
|
31
|
+
shouldMatchAllProps?: boolean;
|
|
32
|
+
}
|
|
33
|
+
declare function toHaveAnimatedStyle(this: MatcherContext, received: unknown, expectedStyle: Record<string, unknown>, config?: AnimatedStyleConfig): MatcherResult;
|
|
34
|
+
declare function toHaveAnimatedProps(this: MatcherContext, received: unknown, expectedProps: Record<string, unknown>): MatcherResult;
|
|
35
|
+
/** All reanimated-compatible matchers, keyed by name for `expect.extend`. */
|
|
36
|
+
declare const animatedMatchers: {
|
|
37
|
+
toHaveAnimatedStyle: typeof toHaveAnimatedStyle;
|
|
38
|
+
toHaveAnimatedProps: typeof toHaveAnimatedProps;
|
|
39
|
+
};
|
|
40
|
+
declare module "vitest" {
|
|
41
|
+
interface Assertion<T = any> {
|
|
42
|
+
toHaveAnimatedStyle(style: Record<string, unknown>, config?: AnimatedStyleConfig): T;
|
|
43
|
+
toHaveAnimatedProps(props: Record<string, unknown>): T;
|
|
44
|
+
}
|
|
45
|
+
interface AsymmetricMatchersContaining {
|
|
46
|
+
toHaveAnimatedStyle(style: Record<string, unknown>, config?: AnimatedStyleConfig): unknown;
|
|
47
|
+
toHaveAnimatedProps(props: Record<string, unknown>): unknown;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { AnimatedStyleConfig, animatedMatchers, toHaveAnimatedProps, toHaveAnimatedStyle };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/matchers/animated.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reanimated-compatible matchers: `toHaveAnimatedStyle` and `toHaveAnimatedProps`.
|
|
4
|
+
*
|
|
5
|
+
* react-native-reanimated ships these matchers via its Jest setup
|
|
6
|
+
* (`setUpTests()`), so they are unavailable under Vitest by default — calling
|
|
7
|
+
* `expect(node).toHaveAnimatedStyle(...)` throws `Invalid Chai property`.
|
|
8
|
+
*
|
|
9
|
+
* vitest-native's reanimated preset resolves animated hooks synchronously:
|
|
10
|
+
* `useAnimatedStyle(updater)` returns `updater()` and the result is passed
|
|
11
|
+
* through as the element's `style` prop (and `animatedProps` is passed through
|
|
12
|
+
* verbatim). These matchers therefore read straight from the rendered element's
|
|
13
|
+
* props, flattening style arrays the same way React Native does.
|
|
14
|
+
*/
|
|
15
|
+
interface MatcherContext {
|
|
16
|
+
isNot: boolean;
|
|
17
|
+
equals: (a: unknown, b: unknown) => boolean;
|
|
18
|
+
utils: {
|
|
19
|
+
matcherHint: (name: string, received?: string, expected?: string, options?: unknown) => string;
|
|
20
|
+
printExpected: (value: unknown) => string;
|
|
21
|
+
printReceived: (value: unknown) => string;
|
|
22
|
+
diff?: (a: unknown, b: unknown) => string | null;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
interface MatcherResult {
|
|
26
|
+
pass: boolean;
|
|
27
|
+
message: () => string;
|
|
28
|
+
}
|
|
29
|
+
interface AnimatedStyleConfig {
|
|
30
|
+
/** When true, every key of the current style must match `expectedStyle` exactly. */
|
|
31
|
+
shouldMatchAllProps?: boolean;
|
|
32
|
+
}
|
|
33
|
+
declare function toHaveAnimatedStyle(this: MatcherContext, received: unknown, expectedStyle: Record<string, unknown>, config?: AnimatedStyleConfig): MatcherResult;
|
|
34
|
+
declare function toHaveAnimatedProps(this: MatcherContext, received: unknown, expectedProps: Record<string, unknown>): MatcherResult;
|
|
35
|
+
/** All reanimated-compatible matchers, keyed by name for `expect.extend`. */
|
|
36
|
+
declare const animatedMatchers: {
|
|
37
|
+
toHaveAnimatedStyle: typeof toHaveAnimatedStyle;
|
|
38
|
+
toHaveAnimatedProps: typeof toHaveAnimatedProps;
|
|
39
|
+
};
|
|
40
|
+
declare module "vitest" {
|
|
41
|
+
interface Assertion<T = any> {
|
|
42
|
+
toHaveAnimatedStyle(style: Record<string, unknown>, config?: AnimatedStyleConfig): T;
|
|
43
|
+
toHaveAnimatedProps(props: Record<string, unknown>): T;
|
|
44
|
+
}
|
|
45
|
+
interface AsymmetricMatchersContaining {
|
|
46
|
+
toHaveAnimatedStyle(style: Record<string, unknown>, config?: AnimatedStyleConfig): unknown;
|
|
47
|
+
toHaveAnimatedProps(props: Record<string, unknown>): unknown;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { AnimatedStyleConfig, animatedMatchers, toHaveAnimatedProps, toHaveAnimatedStyle };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//#region src/matchers/animated.ts
|
|
2
|
+
/** Flatten a React Native `style` prop (object, array, or nested arrays). */
|
|
3
|
+
function flattenStyle(style) {
|
|
4
|
+
if (!style) return {};
|
|
5
|
+
if (Array.isArray(style)) return style.reduce((acc, entry) => Object.assign(acc, flattenStyle(entry)), {});
|
|
6
|
+
if (typeof style === "object") return style;
|
|
7
|
+
return {};
|
|
8
|
+
}
|
|
9
|
+
/** Narrow an unknown value to something with a `props` bag. */
|
|
10
|
+
function getProps(received) {
|
|
11
|
+
if (received && typeof received === "object" && "props" in received) {
|
|
12
|
+
const props = received.props;
|
|
13
|
+
if (props && typeof props === "object") return props;
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
function notAnElementResult(ctx, matcherName, received) {
|
|
18
|
+
return {
|
|
19
|
+
pass: false,
|
|
20
|
+
message: () => `${ctx.utils.matcherHint(matcherName, "element", "expected")}\n\nExpected a rendered element with a \`props\` object, but received:\n ${ctx.utils.printReceived(received)}`
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Pick only the keys present in `expected` from `actual`, for readable diffs. */
|
|
24
|
+
function subset(actual, expected) {
|
|
25
|
+
const result = {};
|
|
26
|
+
for (const key of Object.keys(expected)) result[key] = actual[key];
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
function toHaveAnimatedStyle(received, expectedStyle, config = {}) {
|
|
30
|
+
const props = getProps(received);
|
|
31
|
+
if (!props) return notAnElementResult(this, "toHaveAnimatedStyle", received);
|
|
32
|
+
const currentStyle = flattenStyle(props.style);
|
|
33
|
+
const compared = config.shouldMatchAllProps ? currentStyle : subset(currentStyle, expectedStyle);
|
|
34
|
+
const pass = this.equals(compared, expectedStyle);
|
|
35
|
+
return {
|
|
36
|
+
pass,
|
|
37
|
+
message: () => {
|
|
38
|
+
const hint = this.utils.matcherHint("toHaveAnimatedStyle", "element", "expectedStyle", { isNot: this.isNot });
|
|
39
|
+
if (pass) return `${hint}\n\nExpected element not to have animated style:\n ${this.utils.printExpected(expectedStyle)}\nReceived:\n ${this.utils.printReceived(compared)}`;
|
|
40
|
+
const diff = this.utils.diff?.(expectedStyle, compared);
|
|
41
|
+
return diff ? `${hint}\n\n${diff}` : `${hint}\n\nExpected: ${this.utils.printExpected(expectedStyle)}\nReceived: ${this.utils.printReceived(compared)}`;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function toHaveAnimatedProps(received, expectedProps) {
|
|
46
|
+
const props = getProps(received);
|
|
47
|
+
if (!props) return notAnElementResult(this, "toHaveAnimatedProps", received);
|
|
48
|
+
const compared = subset(props.animatedProps && typeof props.animatedProps === "object" ? props.animatedProps : props, expectedProps);
|
|
49
|
+
const pass = this.equals(compared, expectedProps);
|
|
50
|
+
return {
|
|
51
|
+
pass,
|
|
52
|
+
message: () => {
|
|
53
|
+
const hint = this.utils.matcherHint("toHaveAnimatedProps", "element", "expectedProps", { isNot: this.isNot });
|
|
54
|
+
if (pass) return `${hint}\n\nExpected element not to have animated props:\n ${this.utils.printExpected(expectedProps)}\nReceived:\n ${this.utils.printReceived(compared)}`;
|
|
55
|
+
const diff = this.utils.diff?.(expectedProps, compared);
|
|
56
|
+
return diff ? `${hint}\n\n${diff}` : `${hint}\n\nExpected: ${this.utils.printExpected(expectedProps)}\nReceived: ${this.utils.printReceived(compared)}`;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** All reanimated-compatible matchers, keyed by name for `expect.extend`. */
|
|
61
|
+
const animatedMatchers = {
|
|
62
|
+
toHaveAnimatedStyle,
|
|
63
|
+
toHaveAnimatedProps
|
|
64
|
+
};
|
|
65
|
+
//#endregion
|
|
66
|
+
export { animatedMatchers, toHaveAnimatedProps, toHaveAnimatedStyle };
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// The native boundary: the small set of React Native modules that touch native
|
|
2
|
+
// code, replaced with mocks. Everything ELSE in RN runs for real. Modules are
|
|
3
|
+
// expressed as CJS source strings so both transform hooks (loader + require) can
|
|
4
|
+
// serve them identically. Mirrors react-native/jest/setup.js's mock set.
|
|
5
|
+
|
|
6
|
+
const DEVICE_CONSTANTS = JSON.stringify({
|
|
7
|
+
PlatformConstants: {
|
|
8
|
+
forceTouchAvailable: false,
|
|
9
|
+
reactNativeVersion: { major: 0, minor: 84, patch: 1 },
|
|
10
|
+
osVersion: "17.0",
|
|
11
|
+
systemName: "iOS",
|
|
12
|
+
interfaceIdiom: "phone",
|
|
13
|
+
isTesting: true,
|
|
14
|
+
},
|
|
15
|
+
DeviceInfo: {
|
|
16
|
+
Dimensions: {
|
|
17
|
+
window: { width: 390, height: 844, scale: 3, fontScale: 1 },
|
|
18
|
+
screen: { width: 390, height: 844, scale: 3, fontScale: 1 },
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
I18nManager: {
|
|
22
|
+
isRTL: false,
|
|
23
|
+
doLeftAndRightSwapInRTL: true,
|
|
24
|
+
localeIdentifier: "en_US",
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// A reusable mock-native-component factory, inlined into each source string that needs it.
|
|
29
|
+
//
|
|
30
|
+
// For RCTScrollView we drop the scroll-responder negotiation props that real
|
|
31
|
+
// ScrollView attaches to its host (onStartShouldSetResponder &co). On a device
|
|
32
|
+
// those drive the native gesture/responder system, which doesn't exist in tests.
|
|
33
|
+
// More importantly, RNTL treats any host with onStartShouldSetResponder as a touch
|
|
34
|
+
// responder and then gates events on its return value — RN's ScrollView returns
|
|
35
|
+
// false, which makes RNTL's fireEvent.scroll a no-op. RN's own jest preset sidesteps
|
|
36
|
+
// this by mocking ScrollView so the host never receives these props; we match that
|
|
37
|
+
// effect at the host while keeping the real ScrollView component (so FlatList /
|
|
38
|
+
// SectionList / VirtualizedList behavior stays intact).
|
|
39
|
+
const MOCK_NATIVE_COMPONENT = `
|
|
40
|
+
const React = require("react");
|
|
41
|
+
let __tag = 1;
|
|
42
|
+
const __SCROLL_RESPONDER_PROPS = [
|
|
43
|
+
"onStartShouldSetResponder", "onStartShouldSetResponderCapture",
|
|
44
|
+
"onMoveShouldSetResponder", "onMoveShouldSetResponderCapture",
|
|
45
|
+
];
|
|
46
|
+
// RN's real TextInput renders its native input host as RCTSinglelineTextInputView /
|
|
47
|
+
// RCTMultilineTextInputView (iOS) or AndroidTextInput. But RNTL's host detection
|
|
48
|
+
// and userEvent (type/clear) + getByPlaceholderText key on the host name "TextInput"
|
|
49
|
+
// — the name jest's RN preset produces by mocking TextInput. Render these native
|
|
50
|
+
// input hosts as "TextInput" so RNTL recognises them, matching the jest preset.
|
|
51
|
+
const __TEXT_INPUT_VIEWS = new Set([
|
|
52
|
+
"RCTSinglelineTextInputView", "RCTMultilineTextInputView", "AndroidTextInput",
|
|
53
|
+
"RCTUITextField", "RCTUITextView",
|
|
54
|
+
]);
|
|
55
|
+
const mockNativeComponent = (viewName) => {
|
|
56
|
+
const hostName = __TEXT_INPUT_VIEWS.has(viewName) ? "TextInput" : viewName;
|
|
57
|
+
const C = class extends React.Component {
|
|
58
|
+
constructor(p) { super(p); this._nativeTag = __tag++; }
|
|
59
|
+
render() {
|
|
60
|
+
let props = this.props;
|
|
61
|
+
if (viewName === "RCTScrollView") {
|
|
62
|
+
props = Object.assign({}, this.props);
|
|
63
|
+
for (const k of __SCROLL_RESPONDER_PROPS) delete props[k];
|
|
64
|
+
}
|
|
65
|
+
return React.createElement(hostName, props, props.children);
|
|
66
|
+
}
|
|
67
|
+
blur() {} focus() {} measure() {} measureInWindow() {} measureLayout() {} setNativeProps() {}
|
|
68
|
+
};
|
|
69
|
+
C.displayName = viewName === "RCTView" ? "View" : hostName;
|
|
70
|
+
return C;
|
|
71
|
+
};
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
const TURBO_STUB = `
|
|
75
|
+
const __C = ${DEVICE_CONSTANTS};
|
|
76
|
+
// Native methods that return a Promise on the device (no callback arg). Without
|
|
77
|
+
// this, real RN code doing \`NativeModule.canOpenURL(url).then(...)\` would crash
|
|
78
|
+
// on \`undefined\`. Values are the no-native defaults.
|
|
79
|
+
const __ASYNC = {
|
|
80
|
+
// Linking
|
|
81
|
+
canOpenURL: false, getInitialURL: null, openURL: undefined, openSettings: undefined,
|
|
82
|
+
sendIntent: undefined, getString: "", getInitialState: null,
|
|
83
|
+
// Share
|
|
84
|
+
share: { action: "dismissedAction", activityType: null },
|
|
85
|
+
// Image loader (getSize resolves to a [width, height] TUPLE — RN destructures it)
|
|
86
|
+
getSize: [0, 0], getSizeWithHeaders: { width: 0, height: 0 },
|
|
87
|
+
prefetchImage: true, prefetchImageWithMetadata: true, queryCache: {},
|
|
88
|
+
};
|
|
89
|
+
// RN callback conventions are inconsistent: most native methods are success-first
|
|
90
|
+
// (e.g. getCurrentVoiceOverState(success, error)), but a few are error-first
|
|
91
|
+
// (e.g. showShareActionSheetWithOptions(options, error, success)). For the latter,
|
|
92
|
+
// the success callback is the LAST function arg.
|
|
93
|
+
const __SUCCESS_LAST = new Set(["showShareActionSheetWithOptions"]);
|
|
94
|
+
const turboStub = (name) => new Proxy({}, {
|
|
95
|
+
get: (_t, p) => {
|
|
96
|
+
if (p === "getConstants") return () => (__C[name] || {});
|
|
97
|
+
if (p === "getColorScheme") return () => "light"; // NativeAppearance
|
|
98
|
+
if (p === "addListener") return () => ({ remove: () => {} });
|
|
99
|
+
if (p === "removeListeners") return () => {};
|
|
100
|
+
return (...args) => {
|
|
101
|
+
// Callback-style native methods resolve via a callback argument. Invoke the
|
|
102
|
+
// success callback so JS Promises that wrap these settle instead of hanging.
|
|
103
|
+
const fns = args.filter((a) => typeof a === "function");
|
|
104
|
+
if (fns.length) {
|
|
105
|
+
const cb = typeof p === "string" && __SUCCESS_LAST.has(p) ? fns[fns.length - 1] : fns[0];
|
|
106
|
+
return cb(false);
|
|
107
|
+
}
|
|
108
|
+
// Promise-returning native methods must yield a Promise, not undefined.
|
|
109
|
+
if (typeof p === "string" && Object.prototype.hasOwnProperty.call(__ASYNC, p)) {
|
|
110
|
+
return Promise.resolve(__ASYNC[p]);
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
export const BOUNDARY_SOURCES = {
|
|
119
|
+
"Libraries/TurboModule/TurboModuleRegistry.js": `
|
|
120
|
+
${TURBO_STUB}
|
|
121
|
+
exports.get = (n) => turboStub(n);
|
|
122
|
+
exports.getEnforcing = (n) => turboStub(n);
|
|
123
|
+
`,
|
|
124
|
+
"Libraries/BatchedBridge/NativeModules.js": `
|
|
125
|
+
${TURBO_STUB}
|
|
126
|
+
module.exports = { __esModule: true, default: new Proxy({}, {
|
|
127
|
+
get: (_t, n) => (typeof n === "string" ? turboStub(n) : undefined),
|
|
128
|
+
}) };
|
|
129
|
+
`,
|
|
130
|
+
"Libraries/NativeComponent/NativeComponentRegistry.js": `
|
|
131
|
+
${MOCK_NATIVE_COMPONENT}
|
|
132
|
+
exports.get = (n) => mockNativeComponent(n);
|
|
133
|
+
exports.getWithFallback_DEPRECATED = (n) => mockNativeComponent(n);
|
|
134
|
+
exports.setRuntimeConfigProvider = () => {};
|
|
135
|
+
`,
|
|
136
|
+
"Libraries/ReactNative/requireNativeComponent.js": `
|
|
137
|
+
${MOCK_NATIVE_COMPONENT}
|
|
138
|
+
module.exports = { __esModule: true, default: (n) => mockNativeComponent(n) };
|
|
139
|
+
`,
|
|
140
|
+
"Libraries/Components/View/ViewNativeComponent.js": `
|
|
141
|
+
${MOCK_NATIVE_COMPONENT}
|
|
142
|
+
module.exports = { __esModule: true, default: mockNativeComponent("RCTView"), __INTERNAL_VIEW_CONFIG: {}, Commands: {} };
|
|
143
|
+
`,
|
|
144
|
+
// TextInput: mirror react-native/jest/setup.js, which replaces the real TextInput
|
|
145
|
+
// with a passthrough host component (jest/mocks/TextInput, via mockComponent).
|
|
146
|
+
// The REAL TextInput's internal _onChange calls onChangeText from the native
|
|
147
|
+
// 'change' event — so running it under RNTL's userEvent.type (which dispatches
|
|
148
|
+
// BOTH 'change' and 'changeText' per keystroke) fires onChangeText TWICE. The
|
|
149
|
+
// passthrough mock puts props (incl. onChangeText) directly on the host, so each
|
|
150
|
+
// event fires its own handler once, matching jest-preset and real single-fire
|
|
151
|
+
// semantics. (Verified by the differential cross-check.)
|
|
152
|
+
"Libraries/Components/TextInput/TextInput.js": `
|
|
153
|
+
const React = require("react");
|
|
154
|
+
class TextInput extends React.Component {
|
|
155
|
+
blur() {} focus() {} clear() {}
|
|
156
|
+
isFocused() { return false; }
|
|
157
|
+
getNativeRef() { return null; }
|
|
158
|
+
measure() {} measureInWindow() {} measureLayout() {} setNativeProps() {}
|
|
159
|
+
render() { return React.createElement("TextInput", this.props, this.props.children); }
|
|
160
|
+
}
|
|
161
|
+
TextInput.displayName = "TextInput";
|
|
162
|
+
// Static API some code touches (TextInput.State.currentlyFocusedInput(), …).
|
|
163
|
+
TextInput.State = {
|
|
164
|
+
currentlyFocusedInput: () => null,
|
|
165
|
+
currentlyFocusedField: () => null,
|
|
166
|
+
focusTextInput: () => {},
|
|
167
|
+
blurTextInput: () => {},
|
|
168
|
+
};
|
|
169
|
+
module.exports = { __esModule: true, default: TextInput };
|
|
170
|
+
`,
|
|
171
|
+
"Libraries/Core/InitializeCore.js": `module.exports = { __esModule: true, default: {} };`,
|
|
172
|
+
// AppContainer (which RNTL's render mounts) renders <LogBoxNotificationContainer/>
|
|
173
|
+
// in dev. That component subscribes to LogBoxData and, in componentDidMount,
|
|
174
|
+
// schedules a setTimeout/ setImmediate setState — which fires AFTER the test's
|
|
175
|
+
// act() completes, producing the cosmetic "update to LogBoxStateSubscription not
|
|
176
|
+
// wrapped in act()" warning. It's dev-only notification UI with no role in tests,
|
|
177
|
+
// so stub it to render nothing. This removes the only mount of LogBoxStateSubscription,
|
|
178
|
+
// eliminating the out-of-act update at its source (jest's RN preset mocks LogBox similarly).
|
|
179
|
+
"Libraries/LogBox/LogBoxNotificationContainer.js": `
|
|
180
|
+
module.exports = { __esModule: true, default: function LogBoxNotificationContainer() { return null; } };
|
|
181
|
+
`,
|
|
182
|
+
// RendererProxy re-exports RendererImplementation, which loads RN's native Fabric
|
|
183
|
+
// renderer (ReactNativeRenderer-dev.js) — that version-asserts react vs the bundled
|
|
184
|
+
// react-native-renderer and breaks SectionList/VirtualizedList. react-test-renderer
|
|
185
|
+
// does the real reconciliation, so we only need these imperative helpers as stubs.
|
|
186
|
+
// Mirrors react-native/jest/mocks/RendererProxy.js.
|
|
187
|
+
"Libraries/ReactNative/RendererProxy.js": `
|
|
188
|
+
module.exports = {
|
|
189
|
+
__esModule: true,
|
|
190
|
+
findNodeHandle: () => null,
|
|
191
|
+
findHostInstance_DEPRECATED: () => null,
|
|
192
|
+
dispatchCommand: () => {},
|
|
193
|
+
sendAccessibilityEvent: () => {},
|
|
194
|
+
getNodeFromInternalInstanceHandle: () => null,
|
|
195
|
+
getPublicInstanceFromInternalInstanceHandle: () => null,
|
|
196
|
+
getPublicInstanceFromRootTag: () => null,
|
|
197
|
+
isChildPublicInstance: () => false,
|
|
198
|
+
isProfilingRenderer: () => false,
|
|
199
|
+
renderElement: () => {},
|
|
200
|
+
unmountComponentAtNodeAndRemoveContainer: () => {},
|
|
201
|
+
unstable_batchedUpdates: (fn, a) => fn(a),
|
|
202
|
+
};
|
|
203
|
+
`,
|
|
204
|
+
"Libraries/ReactNative/UIManager.js": `
|
|
205
|
+
module.exports = { __esModule: true, default: new Proxy(
|
|
206
|
+
{ getViewManagerConfig: () => ({}), hasViewManagerConfig: () => true, getConstants: () => ({}) },
|
|
207
|
+
{ get: (t, p) => (p in t ? t[p] : () => undefined) }
|
|
208
|
+
) };
|
|
209
|
+
`,
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const SUFFIXES = Object.keys(BOUNDARY_SOURCES);
|
|
213
|
+
|
|
214
|
+
/** Normalised-path test: is this a native-boundary module? */
|
|
215
|
+
export function isBoundary(normPath) {
|
|
216
|
+
return SUFFIXES.some((s) => normPath.endsWith("/react-native/" + s));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Returns the CJS source for a boundary module, or null if not a boundary. */
|
|
220
|
+
export function boundarySourceFor(normPath) {
|
|
221
|
+
for (const s of SUFFIXES) {
|
|
222
|
+
if (normPath.endsWith("/react-native/" + s)) return BOUNDARY_SOURCES[s];
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
}
|