CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-expo

A Jest preset to painlessly test your Expo / React Native apps.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

testing-utilities.mddocs/

Testing Utilities

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.

Capabilities

Property Mocking

Utilities for temporarily replacing object properties during tests with automatic restoration capabilities.

Mock Property

/**
 * 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:

  • Saves original property descriptor for restoration
  • Handles configurable, enumerable, and writable attributes
  • Supports any object type and property value
  • Maintains property metadata for proper restoration

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");
  });
});

Unmock Property

/**
 * 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:

  • Restores original property descriptor exactly
  • Handles properties that didn't exist originally (removes them)
  • Cleans up internal references automatically
  • Safe to call multiple times

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);
});

Unmock All Properties

/**
 * Restore all mocked properties globally across all objects
 */
function unmockAllProperties(): void;

Features:

  • Restores all properties mocked with mockProperty
  • Works across all objects and test suites
  • Useful for test cleanup and preventing test interference
  • Clears all internal tracking references

Usage 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...
  });
});

React Native API Mocking

Specialized utilities for mocking React Native APIs, particularly the Linking API for navigation testing.

Mock Linking

/**
 * 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:

  • Mocks Linking.addEventListener and Linking.removeEventListener
  • Provides event dispatcher for simulating link events
  • Manages listener registration and cleanup
  • Returns subscription objects with remove() method

Usage 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();
  });
});

Advanced Usage Patterns

Conditional Mocking

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
  });
});

Nested Property Mocking

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
});

Mock Chaining

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
    });
  });
});

Types

// 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;
}

docs

configuration.md

index.md

presets.md

rsc.md

testing-utilities.md

tile.json