Mocking and spying utilities for TSpec testing framework
ℹ️ Important Note
Provides
fn()
andspyOn()
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.
npm install --save-dev @tspec/core @tspec/assert @tspec/mock
- 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
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();
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();
});
});
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
- 📖 Complete Documentation - Full usage guide and API reference
- 🚀 Getting Started Guide - Step-by-step setup
- 💡 Examples - Real-world usage examples
- 🐛 Issues - Report bugs or request features
We welcome contributions! Please see our Contributing Guide for details on:
- Setting up the development environment
- Running tests and ensuring quality
- Submitting pull requests
MIT License - see the LICENSE file for details.