@tspec/mock
v1.5.0
Published
Mocking utilities for TSpec testing framework
Downloads
23
Readme
@tspec/mock
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.
Installation
npm install --save-dev @tspec/core @tspec/assert @tspec/mockFeatures
- 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
- 📖 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
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.
