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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@skuba-lib/vitest-koa-mocks

v1.0.0

Published

Vitest mocks for Koa

Readme

@skuba-lib/vitest-koa-mocks

npm package Node.js version

Vitest-compatible Koa mocks for testing Koa middleware and applications.

Inspired by Shopify's jest-koa-mocks, this package provides utilities that do not rely on Jest globals and are suitable for ESM-based Vitest setups.

API reference

createMockContext

Create a fully-typed mock Koa Context for testing middleware and request handlers.

describe('createServiceAuthClient', () => {
  const serviceAuthClient = createServiceAuthClient({
    audience: 'upstream-service',
    baseUrl,
  });

  it('attaches expected headers', async () => {
    nock(baseUrl)
      .get('/mocked')
      // ensures authorization is passed through
      .matchHeader('authorization', MOCK_SERVICE_AUTH_HEADER)
      // ensures data tags are passed through
      .matchHeader('seek-tag-record-expiry', '0000-01-01T00:00:00.000Z')
      .matchHeader('seek-tag-test-record', 'true')
      // ensures tracing headers are passed through
      .matchHeader('x-request-id', 'abc')
      .matchHeader('x-session-id', 'cba')
      .reply(200);

    const ctx = createMockContext({
      headers: {
        'seek-tag-record-expiry': '0000-01-01T00:00:00.000Z',
        'seek-tag-test-record': 'true',
        'x-request-id': 'abc',
        'x-seek-jwt-issuer': 'requesting-service',
        'x-session-id': 'cba',
      },
    });

    contextStorage.enterWith(ctx);

    await Middleware.seekTagMiddleware(
      { ...ctx, headers: { ...ctx.headers } },
      () =>
        serviceAuthClient.request({
          url: '/mocked',
        }),
    );
  });
});

createMockContext accepts an options object that lets you control:

  • HTTP details such as method, url, statusCode, headers, host, and whether the request is encrypted
  • Koa-specific behaviour such as state, session, cookies, and throw / redirect handlers
  • arbitrary customProperties that are merged onto the returned context

The returned context type is MockContext, which extends Koa's Context with:

  • a cookies implementation compatible with MockCookies
  • a request that can include body, rawBody, and session metadata

createMockCookies

Create a standalone mock cookies implementation for Koa-style code.

import { createMockCookies } from '@skuba-lib/vitest-koa-mocks';
import { expect, it } from 'vitest';

it('tracks request and response cookies', () => {
  const cookies = createMockCookies({ session: 'abc' });

  expect(cookies.get('session')).toBe('abc');

  cookies.set('session', 'def');

  // requestStore reflects initial inbound cookies
  expect(cookies.requestStore.get('session')).toBe('abc');

  // responseStore reflects cookies set during the test
  expect(cookies.responseStore.get('session')).toBe('def');
});

createMockCookies returns a MockCookies instance that:

  • exposes get and set methods implemented with Vitest spies (vi.fn)
  • maintains separate requestStore and responseStore Map instances for assertions
  • supports a secure flag to simulate HTTPS behaviour