Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/vitest-react-native. Run React Native unit and component tests with Vitest
Something went wrong. Try again.
1.2 kB · 43 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344import { createRequire } from 'node:module';import { afterAll, afterEach, expect, vi } from 'vitest';
import { installRuntime } from './runtime.js';
import type { RuntimeOptions } from './types.js';
const options: RuntimeOptions = JSON.parse(process.env.VITEST_REACT_NATIVE_OPTIONS ?? '{}');
const root = process.env.VITEST_REACT_NATIVE_ROOT ?? process.cwd();
const require = createRequire(`${root}/package.json`);
Object.assign(globalThis, { expect, jest: vi });
const dispose = installRuntime(root, options);
/** * RNTL is an optional integration. Register its cleanup only when the app uses it. * Keep resolution separate from loading so failures inside RNTL are not swallowed. */let testingLibrary: string | undefined;
try { testingLibrary = require.resolve('@testing-library/react-native/pure');} catch (error) { if ((error as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') { throw error; }}
if (testingLibrary) { const { cleanup } = require(testingLibrary) as Pick< typeof import('@testing-library/react-native'), 'cleanup' >;
afterEach(async () => { await cleanup(); });}
afterAll(dispose);