A Jest preset to painlessly test your Expo / React Native apps.
npx @tessl/cli install tessl/npm-jest-expo@54.0.0Jest Expo provides a comprehensive Jest testing preset specifically designed for Expo and React Native applications, enabling seamless testing across multiple platforms including iOS, Android, web, and Node.js environments. It extends React Native's Jest preset with Expo-specific configurations, module name mappings, custom transform patterns for asset files, and platform-specific test execution capabilities.
npm install jest-expo// Main testing utilities
import { mockProperty, unmockProperty, unmockAllProperties, mockLinking } from "jest-expo";For CommonJS:
const { mockProperty, unmockProperty, unmockAllProperties, mockLinking } = require("jest-expo");Configure Jest in your package.json or jest.config.js:
{
"jest": {
"preset": "jest-expo"
}
}Jest Expo provides a Jest binary proxy (bin/jest) that ensures consistent Jest execution across different package managers. The proxy forwards all arguments and stdio streams to the actual Jest binary, solving issues where package managers don't hoist binaries from nested dependencies:
# Run tests using the jest-expo binary proxy
npx jest
# Or if configured in package.json scripts:
npm test
# The proxy supports all Jest CLI arguments
npx jest --watch --coverageFeatures:
{
"jest": {
"preset": "jest-expo/universal"
}
}import { mockProperty, unmockProperty } from "jest-expo";
describe("My Component", () => {
beforeEach(() => {
// Mock a property for testing
mockProperty(console, "warn", jest.fn());
});
afterEach(() => {
// Restore original property
unmockProperty(console, "warn");
});
test("should work correctly", () => {
// Your test code here
});
});Jest Expo is built around several key components:
Core Jest preset configurations for different platforms and testing scenarios. Includes base preset with Expo-specific module mappings and platform-specific presets for targeted testing.
// Available preset configurations (configured in package.json or jest.config.js)
"jest-expo" // Base preset with Expo configurations
"jest-expo/universal" // Multi-platform testing preset
"jest-expo/ios" // iOS-specific testing preset
"jest-expo/android" // Android-specific testing preset
"jest-expo/web" // Web-specific testing preset
"jest-expo/node" // Node.js/SSR testing presetProperty mocking utilities for controlling object properties during tests, and React Native Linking API mocking for navigation testing.
function mockProperty<T>(object: T, property: keyof T, mockValue: any): void;
function unmockProperty<T>(object: T, property: keyof T): void;
function unmockAllProperties(): void;
function mockLinking(): (eventName: string, eventData: any) => void;Advanced testing support for React Server Components, including RSC-specific presets and custom Jest matchers for flight data validation.
// RSC preset configurations
"jest-expo/rsc" // RSC universal preset
"jest-expo/rsc/ios" // RSC iOS preset
"jest-expo/rsc/android" // RSC Android preset
"jest-expo/rsc/web" // RSC web preset
// RSC Jest matchers
interface JestExpoMatchers<R> {
toMatchFlight(expectedFlight: string): R;
toMatchFlightSnapshot(): R;
}Advanced configuration utilities for customizing Jest presets and watch plugins for enhanced development experience.
// Configuration functions (from jest-expo/config)
function getWebPreset(options?: PresetOptions): JestConfig;
function getIOSPreset(options?: PresetOptions): JestConfig;
function getAndroidPreset(options?: PresetOptions): JestConfig;
function getNodePreset(options?: PresetOptions): JestConfig;
function getPlatformPreset(displayOptions?: DisplayOptions, extensions?: string[], platform?: string, options?: { isServer?: boolean; isReactServer?: boolean }): JestConfig;
function withWatchPlugins(jestConfig: JestConfig): JestConfig;
function getWatchPlugins(jestConfig: JestConfig): WatchPlugin[];interface JestExpoMatchers<R> extends Record<string, any> {
toMatchFlight(expectedFlight: string): R;
toMatchFlightSnapshot(): R;
}
interface PresetOptions {
displayOptions?: DisplayOptions;
extensions?: string[];
platform?: string;
}
interface DisplayOptions {
name?: string;
color?: string;
}