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

tanstack-mock-server-fn

v0.2.3

Published

Mock server functions for TanStack Start server functions

Readme

tanstack-mock-server-fn

A lightweight utility for mocking server functions in your stories and tests.

Installation

npm install tanstack-mock-server-fn
# or
pnpm add tanstack-mock-server-fn
# or
yarn add tanstack-mock-server-fn

Vite Integration

To enable server function mocking, add the Vite plugin to your configuration. The plugin automatically inspects your code to locate server functions (which must be created via a call chain starting with createServerFn by default, or a custom name you configure) and rewrites them to delegate to your registered mocks.

Configuration Options:

  • enabled: (boolean, default: true) - Whether the plugin is active. If false, the plugin skips rewriting, and the real server function is used.
  • debug: (boolean, default: false) - Whether to enable debug logging in the console during development.
  • serverFnName: (string, default: "createServerFn") - The function name used to detect server function creation. This option allows you to specify a different function name if your server function creator is named something other than "createServerFn".

Example Configuration:

// vite.config.ts
import { defineConfig } from "vite";
import { serverFnOverridePlugin } from "tanstack-mock-server-fn/vite";

export default defineConfig({
  plugins: [
    serverFnOverridePlugin({
      enabled: true,                 // Enable for stories/tests, disable for production
      debug: false,                  // Optional: Enable debug logging during development
      serverFnName: "createServerFn", // Optional: Customize the server function creator name
    }),
  ],
});

If you use a different function name to create your server functions, for example, myServerFnCreator, you would configure the plugin like this:

// vite.config.ts
import { defineConfig } from "vite";
import { serverFnOverridePlugin } from "tanstack-mock-server-fn/vite";

export default defineConfig({
  plugins: [
    serverFnOverridePlugin({
      enabled: true,
      debug: true,
      serverFnName: "myServerFnCreator", // Use "myServerFnCreator" instead of "createServerFn"
    }),
  ],
});

Mocking Server Functions

In your stories or tests, you can easily mock your server functions. Import the mockServerFn helper from this package and register your mock implementation. Importantly, you pass exactly two arguments—the function to be mocked and your mock implementation. The plugin automatically injects the function's name as a third parameter, ensuring correct linkage.

// my-story.ts
import { mockServerFn, clearMocks } from "tanstack-mock-server-fn";
import { getUsers } from "./api";
import { mockUserData } from "./test-data";

// Register a mock implementation for the getUsers server function
mockServerFn(getUsers, async (args) => {
  console.log("Using mock implementation for getUsers");
  return mockUserData;
});

// Later, if necessary, you can clear all registered mocks
clearMocks();

When the server function getUsers is invoked in your testing environment, the Vite plugin transforms it into something like this:

(args) => {
  const mockImpl = getMockImplementation("getUsers");
  if (mockImpl)
    return mockImpl(args ?? {});
  throw new Error("No mock implementation found for getUsers");
}

If no mock is found, an error will be thrown. This ensures that you are always explicitly handling the behavior of your mocked server functions.

Advanced API

Internally, the package uses a global registry to store mock implementations. In addition to mockServerFn and clearMocks, you can access two additional helpers:

  • Check if a mock exists:

    import { hasMock } from "tanstack-mock-server-fn";
    if (hasMock("getUsers")) {
      console.log("Mock for getUsers is registered");
    }
  • Directly fetch the mock implementation:

    import { getMockImplementation } from "tanstack-mock-server-fn";
    const mock = getMockImplementation("getUsers");

How It Works

  1. Runtime Registry: A global registry stores your mock implementations, keyed by the function name.
  2. Vite Plugin Transformation: The plugin scans your code for server functions (identified by a call chain beginning with createServerFn), and rewrites them to delegate to a registered mock.

This design allows you to seamlessly switch between real and mock implementations, ensuring that only your tests and stories use the mocks while your production code remains untouched.

License

MIT