@tspec/mock
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

@tspec/mock

Mocking and spying utilities for TSpec testing framework

ℹ️ Important Note

Provides fn() and spyOn() for creating mocks and spies in your tests.

This package is designed to work as part of the TSpec ecosystem and Used alongside @tspec/core and @tspec/assert for complete testing.

📖 View complete documentation and examples →

Installation

npm install --save-dev @tspec/core @tspec/assert @tspec/mock

Features

  • Function mocks with fn() for creating test doubles
  • Method spying with spyOn() for existing objects
  • Mock implementations and return value control
  • Call tracking and verification utilities

Usage

import { fn, spyOn } from '@tspec/mock';
import { expect } from '@tspec/assert';

// Create mock functions
const mockCallback = fn().mockReturnValue('mocked result');
expect(mockCallback()).toBe('mocked result');

// Spy on existing methods
const consoleSpy = spyOn(console, 'log');
console.log('test message');
expect(consoleSpy).toHaveBeenCalledWith('test message');
consoleSpy.mockRestore();

Complete Testing Example

import { describe, test } from '@tspec/core';
import { expect } from '@tspec/assert';
import { fn, spyOn } from '@tspec/mock';

describe('mock Example', () => {
  test('demonstrates mock functionality', () => {
    
    
    // Mocking and spying
    const mockFn = fn().mockReturnValue('mocked');
    expect(mockFn()).toBe('mocked');
    expect(mockFn).toHaveBeenCalled();
  });
});

Related Packages

TSpec is designed as a cohesive framework. Here are the related packages:

  • @tspec/core - Core testing framework functionality for TSpec
  • @tspec/assert - Assertion library for TSpec testing framework

Documentation & Support

Contributing

We welcome contributions! Please see our Contributing Guide for details on:

  • Setting up the development environment
  • Running tests and ensuring quality
  • Submitting pull requests

License

MIT License - see the LICENSE file for details.


Package Sidebar

Install

npm i @tspec/mock

Weekly Downloads

8

Version

1.5.0

License

MIT

Unpacked Size

24.4 kB

Total Files

15

Last publish

Collaborators

  • rchmn333