/** * Vitest configuration for testing an `@effect/ui-native` app in Node. * * **Details** * * {@link plugin} compiles the React Native Flow sources used by `Css` and * `Image`, so tests can import the kit without mocking React Native imports. * It supports the kit's non-React dependencies, not React Native's React * components. * * Requires Node 22.15 or later. Install `flow-remove-types` and `sucrase` * as dev dependencies before using the plugin. * * **Example** (Configuring Vitest) * * ```ts * // vitest.config.ts * import { NativeVitest } from "@effect/ui-native/node" * import { defineConfig } from "vitest/config" * * export default defineConfig({ plugins: [NativeVitest.plugin()] }) * ``` * * @stability experimental * @since 4.2.0 */ import * as Path from "node:path" import { fileURLToPath, pathToFileURL } from "node:url" /** * Options accepted by {@link plugin}. * * @stability experimental * @category models * @since 4.2.0 */ export interface Options { /** * Selects React Native's platform files, affecting values such as * `Css.color("red")`. Defaults to `"ios"`. `"macos"` loads react-native-macos, * from the project's root, in place of `react-native`. Does not change the * OS that kit components read from `Native.Device`. */ readonly platform?: "android" | "ios" | "macos" | undefined } /** * The Vite plugin {@link plugin} returns. * * @stability experimental * @category models * @since 4.2.0 */ export interface Plugin { readonly name: string readonly enforce: "pre" readonly resolveId: ( this: { readonly resolve: ( source: string, importer: string | undefined, options: { readonly skipSelf: boolean } ) => Promise<{ readonly id: string } | null> }, source: string, importer: string | undefined, options: object ) => Promise<{ readonly id: string } | null> readonly config: (config: { readonly root?: string | undefined }) => { readonly test: { readonly execArgv: Array readonly server: { readonly deps: { readonly external: Array readonly inline: Array } } } } } /** * Returns a Vitest plugin that compiles the kit's React Native dependencies * for Node tests. * * **Details** * * Each test worker installs Node loader hooks before Vitest starts, so * the app's setup files can import the kit too. React Native dependencies * load through Node; `@effect/ui-native` is inlined for Vitest's CommonJS * interop. Compiled files are cached in * `node_modules/.cache/@effect/ui-native/vitest` under the project's root. * `NativeApp` loads without its Fast Refresh client, which needs Metro. * * @stability experimental * @category constructors * @since 4.2.0 */ export const plugin = (options?: Options | undefined): Plugin => { const platform = options?.platform ?? "ios" let root = Path.resolve() return { name: "@effect/ui-native/vitest", // Ahead of Vite's resolver, which would find react-native itself. enforce: "pre", // Swaps in react-native-macos, as its CLI has Metro do. React Native // before 0.87 has only the `asset-registry` file, not the entry point. async resolveId(source, importer, resolveOptions) { const fork = platform === "macos" && /^react-native(?:\/|$)/.test(source) if (!fork && source !== "react-native/asset-registry") return null const name = fork ? source.replace("react-native", "react-native-macos") : source const from = fork ? Path.join(root, "package.json") : importer const resolve = (id: string) => this.resolve(id, from, { ...resolveOptions, skipSelf: true }).catch( () => null ) return await resolve(name) ?? (name.endsWith("/asset-registry") ? await resolve( name.replace(/asset-registry$/, "Libraries/Image/AssetRegistry") ) : null) }, config: (config) => { root = Path.resolve(config.root ?? "") return { test: { execArgv: [ "--import", `${loader}?${new URLSearchParams({ platform, root, cache: Path.join( root, "node_modules/.cache/@effect/ui-native/vitest" ) })}` ], server: { deps: { external: [ /[\\/]node_modules[\\/](?:react-native(?:-macos)?|@react-native[\\/][^\\/]+)[\\/]/ ], inline: [/[\\/]@effect[\\/]ui-native[\\/]/] } } } } } } } const self = fileURLToPath(import.meta.url) const loader = pathToFileURL( Path.join(Path.dirname(self), "internal/vitest" + Path.extname(self)) ).href