or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdplugin-configuration.mdruntime-api.md
tile.json

runtime-api.mddocs/

Runtime API

The runtime API methods injected into each transformed module by babel-plugin-rewire, enabling dependency mocking and module state manipulation during test execution.

Capabilities

Dependency Rewiring

Get Dependency Value

/**
 * Get the current value of a dependency or variable
 * @param variableName - Name of the variable/dependency to retrieve
 * @returns Current value of the dependency
 */
function __GetDependency__(variableName: string): any;
function __get__(variableName: string): any; // Alias for compatibility

Usage Example:

// module.js
import utils from './utils';
const CONSTANT = 42;

// test.js
import MyModule from './module';

// Get current dependency value
const currentUtils = MyModule.__GetDependency__('utils');
const currentConstant = MyModule.__get__('CONSTANT');

Set Dependency Value

/**
 * Set a new value for a dependency or variable
 * @param variableName - Name of the variable/dependency to set
 * @param value - New value to assign
 * @returns Function to revert the change
 */
function __Rewire__(variableName: string, value: any): () => void;
function __set__(variableName: string, value: any): () => void; // Alias for compatibility

/**
 * Set multiple dependencies at once using an object map
 * @param objectMap - Object containing variable names as keys and new values
 * @returns Function to revert all changes
 */
function __set__(objectMap: Record<string, any>): () => void;

Usage Examples:

// Single dependency
const revert = MyModule.__Rewire__('utils', mockUtils);
// Use mocked version...
revert(); // Restore original

// Multiple dependencies
const revertAll = MyModule.__set__({
  utils: mockUtils,
  CONSTANT: 99
});
// Use mocked versions...
revertAll(); // Restore all originals

// Using returned revert function
it('should mock dependency', () => {
  const revert = MyModule.__set__('apiClient', mockClient);
  
  // Test with mock...
  expect(MyModule.getData()).toBe('mock data');
  
  // Cleanup
  revert();
});

Reset Dependency

/**
 * Reset a dependency back to its original value
 * @param variableName - Name of the variable/dependency to reset
 */
function __ResetDependency__(variableName: string): void;
function __reset__(variableName: string): void; // Alias for compatibility

Usage Example:

// Mock a dependency
MyModule.__Rewire__('database', mockDb);

// Later reset it
MyModule.__ResetDependency__('database');
// database is now back to its original value

Scoped Rewiring

Temporary Rewiring with with

/**
 * Temporarily rewire dependencies for the duration of a callback
 * @param objectMap - Object containing variable names as keys and temporary values
 * @returns Function that accepts a callback to execute with rewired state
 */
function __with__(objectMap: Record<string, any>): (callback: () => any) => any;

Usage Examples:

// Synchronous usage
const result = MyModule.__with__({
  apiUrl: 'http://test-api.com',
  timeout: 1000
})(() => {
  return MyModule.makeRequest(); // Uses temporary values
});
// Values automatically reset after callback

// Asynchronous usage with Promise
const asyncResult = MyModule.__with__({
  database: mockDb
})(async () => {
  return await MyModule.fetchUser(123);
});
// Handles Promise resolution/rejection and cleanup

API Object Access

RewireAPI Object

/**
 * Object containing all rewiring methods
 * Available as named export and property of default export
 */
interface __RewireAPI__ {
  __get__(variableName: string): any;
  __GetDependency__(variableName: string): any;
  __set__(variableName: string, value: any): () => void;
  __set__(objectMap: Record<string, any>): () => void;
  __Rewire__(variableName: string, value: any): () => void;
  __reset__(variableName: string): void;
  __ResetDependency__(variableName: string): void;
  __with__(objectMap: Record<string, any>): (callback: () => any) => any;
}

Usage Examples:

// Import as named export
import { calculate, __RewireAPI__ } from './calculator';

__RewireAPI__.__Rewire__('mathUtils', mockMath);

// Access from default export (if available)
import Calculator from './calculator';

Calculator.__RewireAPI__.__set__('API_KEY', 'test-key');

// Use when you don't want to import the module directly
import CalculatorRewire from './calculator'; // Gets __RewireAPI__ as default

CalculatorRewire.__Rewire__('dependency', mockValue);

Global Reset

Reset All Modules

/**
 * Reset all rewired dependencies across all modules
 * Available in global scope when any module uses babel-plugin-rewire
 */
function __rewire_reset_all__(): void;

Usage Example:

describe('test suite', () => {
  afterEach(() => {
    // Reset all rewired dependencies in all modules
    __rewire_reset_all__();
  });

  it('test 1', () => {
    Module1.__set__('dep1', 'mock1');
    Module2.__set__('dep2', 'mock2');
    // Test with mocked dependencies...
  });

  it('test 2', () => {
    // All dependencies are clean due to afterEach
    Module3.__set__('dep3', 'mock3');
    // Test with fresh state...
  });
});

Export Patterns

ES6 Modules with Default Export

// Original module
export default function myFunction() { /* ... */ }

// After transformation - default export enriched
import MyFunction from './module';

// All methods available on default export
MyFunction.__Rewire__('dependency', mock);
MyFunction.__RewireAPI__.__set__('variable', value);

// Also available as named exports
import { __RewireAPI__, __Rewire__ } from './module';

ES6 Modules without Default Export

// Original module
export function utilA() { /* ... */ }
export function utilB() { /* ... */ }

// After transformation - __RewireAPI__ becomes default export
import ModuleAPI from './module';

ModuleAPI.__Rewire__('dependency', mock);

// Also available as named exports
import { utilA, utilB, __RewireAPI__ } from './module';

CommonJS Modules

// Original module
module.exports = function() { /* ... */ };

// After transformation - module.exports enriched
const MyModule = require('./module');

MyModule.__Rewire__('dependency', mock);
MyModule.__RewireAPI__.__set__('variable', value);

Types

/**
 * Revert function returned by __set__ and __Rewire__
 */
type RevertFunction = () => void;

/**
 * Callback function for __with__ method
 */
type WithCallback = () => any;

/**
 * Object map for bulk rewiring operations
 */
type RewireObjectMap = Record<string, any>;

/**
 * Complete RewireAPI interface
 */
interface RewireAPI {
  __get__(variableName: string): any;
  __GetDependency__(variableName: string): any;
  __set__(variableName: string, value: any): RevertFunction;
  __set__(objectMap: RewireObjectMap): RevertFunction;
  __Rewire__(variableName: string, value: any): RevertFunction;
  __reset__(variableName: string): void;
  __ResetDependency__(variableName: string): void;
  __with__(objectMap: RewireObjectMap): (callback: WithCallback) => any;
}