diff --git a/package.json b/package.json index f5cedb4..af64397 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,10 @@ "types": "./dist/register.d.ts", "default": "./dist/register.js" }, + "./vitest": { + "types": "./dist/vitest/plugin.d.ts", + "default": "./dist/vitest.js" + }, "./package.json": "./package.json" }, "files": [ diff --git a/src/main.ts b/src/main.ts index d1110ee..07d0409 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,2 +1,3 @@ export { mockNative, restoreNativeMocks } from "./lib/mockNative"; + export type { NativeBase, NativeKey } from "./lib/mockNative"; diff --git a/test/env.ts b/src/vitest/env.ts similarity index 73% rename from test/env.ts rename to src/vitest/env.ts index 1fe5107..ca40bc4 100644 --- a/test/env.ts +++ b/src/vitest/env.ts @@ -1,9 +1,12 @@ +import { name } from "../../package.json"; + import type { Environment } from "vitest"; const reactNativeEnv: Environment = { name: "react-native", setup: async () => { - await import("../src/load"); + await import(`${name}/register`); + return { teardown: () => undefined }; }, transformMode: "ssr", diff --git a/src/vitest/plugin.ts b/src/vitest/plugin.ts new file mode 100644 index 0000000..955cef0 --- /dev/null +++ b/src/vitest/plugin.ts @@ -0,0 +1,24 @@ +import type { Plugin } from "vite"; + +export function reactNativePlugin(): Plugin { + return { + config: { + handler: () => ({ + test: { + environment: "./src/vitest/env.ts", + globals: true, + server: { + deps: { + external: [ + "react-native", + "@react-native", + ], + }, + }, + }, + }), + }, + enforce: "pre", + name: "react-native-vite-plugin", + }; +} diff --git a/vite.config.ts b/vite.config.ts index 01e2dcc..2d3df70 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -8,6 +8,7 @@ export default defineConfig({ load: "./src/load.ts", main: "./src/main.ts", register: "./src/register.ts", + vitest: "./src/vitest/plugin.ts", }, fileName: (_, entry) => entry, formats: ["cjs", "es"], @@ -25,6 +26,7 @@ export default defineConfig({ compilerOptions: { emitDeclarationOnly: true, incremental: false, + rootDir: "./src", }, include: ["src/**", "typings/**"], }), diff --git a/vitest.config.ts b/vitest.config.ts index d63e453..09957af 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,18 +1,11 @@ import { defineConfig } from "vitest/config"; +import { reactNativePlugin } from "./src/vitest/plugin"; + export default defineConfig({ + plugins: [reactNativePlugin()], test: { - environment: "./test/env.ts", - globals: true, include: ["test/**/*.test.ts?(x)"], - server: { - deps: { - external: [ - "react-native", - "@react-native", - ], - }, - }, setupFiles: "./test/setup.ts", }, });