npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vitest-automoq

v0.2.0

Published

Type-safe utility functions for mocking methods in Vitest

Downloads

2

Readme

vitest-automoq

Type-safe utility functions for mocking methods in Vitest.

This library provides enhanced type safety and convenience when working with Vitest mocks, handling all type complexities internally so you can focus on writing tests.

Installation

npm install vitest-automoq
# or
pnpm add vitest-automoq
# or
yarn add vitest-automoq

Features

  • 🎯 Type-safe method mocking with full TypeScript support
  • 🔍 Automatic method name detection from function references
  • 🧬 Support for mocking both instance and prototype methods
  • ⚡️ Type-safe mock resolved values for Promise-returning methods
  • 🏭 Convenient method mocker factory
  • 🔄 Bulk mocking of prototype methods with filtering options
  • 🛡️ Zero type configuration needed - just write your tests

API

moqFn

Easy-to-use utility to mock any method. Just pass your instance and a method:

import { moqFn } from 'vitest-automoq';

class UserService {
  async getUser(id: number) {
    return { id, name: 'John' };
  }
}

const service = new UserService();

// Returns a type-safe mock function
const mock = moqFn(service, service.getUser);

// Set up your mock implementation with full type safety
mock.mockResolvedValue({ id: 1, name: 'Mock User' }); // ✅ Type-safe
mock.mockResolvedValue('wrong type'); // ❌ Type error
mock.mockResolvedValue({ id: 1 }); // ❌ Type error - missing name

// Use your mocked service
const user = await service.getUser(1);
console.log(user); // { id: 1, name: 'Mock User' }

// Verify your mock was called
expect(mock).toHaveBeenCalledWith(1);

// Restore all mocks when done
vi.restoreAllMocks();

moqFns

Creates a reusable mocker for cleaner test setup. Perfect for mocking multiple methods on the same instance:

import { moqFns } from 'vitest-automoq';

class DatabaseService {
  async get(id: number) {
    return { id, data: 'real data' };
  }
  
  async save(data: { id: number; value: string }) {
    return true;
  }
}

// Create once, use many times
const service = new DatabaseService();
const mocker = moqFns(service);

// Simple and clean API with full type safety
const getMock = mocker(service.get);
getMock.mockResolvedValue({ id: 1, data: 'mocked' }); // ✅ Type-safe

const saveMock = mocker(service.save);
saveMock.mockResolvedValue(true); // ✅ Type-safe

// Use your mocked service
const result = await service.get(1);
expect(result.data).toBe('mocked');

moqAllPrototypeFns

Bulk mock all methods with smart filtering options:

import { moqAllPrototypeFns } from 'vitest-automoq';

class PaymentService {
  async processPayment(amount: number) {
    return { success: true };
  }
  
  async refund(transactionId: string) {
    return { success: true };
  }
  
  private async logTransaction() {
    // internal logging
  }
}

const paymentService = new PaymentService();

// Mock all methods at once - returns an object of type-safe mocks
const mocks = moqAllPrototypeFns(paymentService);

// All methods are mocked and accessible directly with full type safety
mocks.processPayment.mockResolvedValue({ success: false }); // ✅ Type-safe
mocks.refund.mockResolvedValue({ success: true }); // ✅ Type-safe

// Use your mocked service
const result = await paymentService.processPayment(100);
expect(result.success).toBe(false);

// Clean up when done
vi.restoreAllMocks();

Advanced Usage

Mocking Static Methods

Easily mock static methods:

class Calculator {
  static add(a: number, b: number) {
    return a + b;
  }
}

// Mock static method
const mock = moqFn(Calculator, 'add');
mock.mockReturnValue(100); // ✅ Type-safe

// Use mocked static method
const result = Calculator.add(5, 10);
expect(result).toBe(100);

Filtering Methods to Mock

Selectively mock only the methods you need:

const mocks = moqAllPrototypeFns(
  service,
  (key) => !key.startsWith('_'), // Skip private methods
  true,  // Include inherited methods
  false  // Exclude instance properties
);

Type Safety Without Extra Work

All mocked methods retain their original types with zero configuration:

interface User {
  id: number;
  name: string;
}

class UserRepository {
  async findById(id: number): Promise<User> {
    return db.users.findById(id);
  }
}

const repo = new UserRepository();
const mock = moqFn(repo, repo.findById);

// TypeScript knows this is a User object
mock.mockResolvedValue({ id: 1, name: 'User 1' }); // ✅ Type-safe

// TypeScript prevents incorrect types
mock.mockResolvedValue('wrong type'); // ❌ Type error
mock.mockResolvedValue({ id: 1 }); // ❌ Type error - missing name

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT