A Jest preset to painlessly test your Expo / React Native apps.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Jest Expo provides powerful testing utilities for mocking object properties and React Native APIs during tests. These utilities help create controlled testing environments and mock external dependencies effectively.
Utilities for temporarily replacing object properties during tests with automatic restoration capabilities.
/**
* Mock a property on an object during testing
* @param object - Target object to mock property on
* @param property - Property name to mock
* @param mockValue - Value to replace the property with
*/
function mockProperty<T>(object: T, property: keyof T, mockValue: any): void;Features:
Usage Example:
import { mockProperty, unmockProperty } from "jest-expo";
describe("Console mocking", () => {
beforeEach(() => {
// Mock console.warn to capture warnings
mockProperty(console, "warn", jest.fn());
});
afterEach(() => {
// Restore original console.warn
unmockProperty(console, "warn");
});
test("should not show warnings", () => {
console.warn("This warning is mocked");
expect(console.warn).toHaveBeenCalledWith("This warning is mocked");
});
});/**
* Restore original property on an object
* @param object - Target object to restore property on
* @param property - Property name to restore
*/
function unmockProperty<T>(object: T, property: keyof T): void;Features:
Usage Example:
import { mockProperty, unmockProperty } from "jest-expo";
test("property restoration", () => {
const originalLog = console.log;
mockProperty(console, "log", jest.fn());
expect(console.log).not.toBe(originalLog);
unmockProperty(console, "log");
expect(console.log).toBe(originalLog);
});/**
* Restore all mocked properties globally across all objects
*/
function unmockAllProperties(): void;Features:
mockPropertyUsage Example:
import { mockProperty, unmockAllProperties } from "jest-expo";
describe("Test suite with cleanup", () => {
afterAll(() => {
// Restore all mocked properties at once
unmockAllProperties();
});
test("multiple mocks", () => {
mockProperty(console, "log", jest.fn());
mockProperty(console, "warn", jest.fn());
mockProperty(global, "fetch", jest.fn());
// Test code here...
});
});Specialized utilities for mocking React Native APIs, particularly the Linking API for navigation testing.
/**
* Mock React Native Linking API for testing deep links and navigation
* @returns Event dispatcher function for triggering link events
*/
function mockLinking(): (eventName: string, eventData: any) => void;Features:
Linking.addEventListener and Linking.removeEventListenerremove() methodUsage Example:
import { mockLinking, unmockProperty } from "jest-expo";
import { Linking } from "react-native";
describe("Deep link handling", () => {
let dispatchLinkEvent;
beforeEach(() => {
dispatchLinkEvent = mockLinking();
});
afterEach(() => {
unmockProperty(Linking, "addEventListener");
unmockProperty(Linking, "removeEventListener");
});
test("should handle deep links", () => {
const linkHandler = jest.fn();
// Add event listener
const subscription = Linking.addEventListener("url", linkHandler);
// Simulate receiving a deep link
dispatchLinkEvent("url", { url: "myapp://profile/123" });
expect(linkHandler).toHaveBeenCalledWith({ url: "myapp://profile/123" });
// Clean up
subscription.remove();
});
test("should handle listener removal", () => {
const linkHandler = jest.fn();
Linking.addEventListener("url", linkHandler);
Linking.removeEventListener("url", linkHandler);
// This should not trigger the handler
dispatchLinkEvent("url", { url: "myapp://test" });
expect(linkHandler).not.toHaveBeenCalled();
});
});import { mockProperty, unmockProperty } from "jest-expo";
describe("Conditional API mocking", () => {
test("should mock different APIs based on platform", () => {
if (Platform.OS === "ios") {
mockProperty(StatusBar, "setBarStyle", jest.fn());
} else {
mockProperty(StatusBar, "setBackgroundColor", jest.fn());
}
// Platform-specific test logic
});
});import { mockProperty } from "jest-expo";
test("nested object mocking", () => {
const mockConfig = {
apiUrl: "https://test-api.example.com",
timeout: 5000
};
mockProperty(global, "AppConfig", mockConfig);
// Test code that uses global.AppConfig
});import { mockProperty } from "jest-expo";
describe("Mock chaining pattern", () => {
beforeEach(() => {
// Chain multiple mocks for comprehensive API replacement
mockProperty(fetch, "prototype", {
json: jest.fn().mockResolvedValue({ data: "test" }),
text: jest.fn().mockResolvedValue("test response"),
ok: true,
status: 200
});
});
});// Utility function signatures
declare function mockProperty<T = any>(
object: T,
property: keyof T,
mockValue: any
): void;
declare function unmockProperty<T = any>(
object: T,
property: keyof T
): void;
declare function unmockAllProperties(): void;
declare function mockLinking(): (eventName: string, eventData: any) => void;
// Event dispatcher type
type LinkEventDispatcher = (eventName: string, eventData: any) => void;
// Linking event types
interface LinkingEventData {
url: string;
}
interface LinkingSubscription {
remove(): void;
}