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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocktory

v0.2.2

Published

A mock manager for Node.js applications that combines [msw](https://mswjs.io/) interceptors with Redis-based persistence.

Downloads

239

Readme

Mocktory (beta)

A mock manager for Node.js applications that combines msw interceptors with Redis-based persistence.

Mocktory simplifies mock management for DEV/QA/AQA teams, enabling real-time scenario simulation and system testing.

While MSW lets you define mocks in code, Mocktory supports both code-based and real-time mock definitions.

Features

  • Real-time requests retrospective: track all outgoing HTTP requests from a Node.js application in history.
  • Mocking: manipulate responses in real-time, setting scenarios like failure, success, or passthrough.
  • [WIP]: ability to save responses to storage for later use.
  • [WIP]: group set of mocks into scenarios to simplify test preparation.
  • [WIP]: providing a UI for managing mocks and history.

Temporary UI replaced by Swagger. 👉 See live example about Mocktory features

Roadmap

  • [WIP] Offer default enhancers for feature IDs that have unique factors apart from the basic URL.

v1 release

  • Transform Swagger to a nice UI.
  • Provide more granular control of history TTL and other system parameters.

v1+ releases

  • Allow saving requests to custom storage (like S3).
  • Get rid of MSW and use smaller @mswjs/interceptors

Installation

npm install mocktory

Usage

Enable Mocktory in your application

import { MockService } from 'mocktory'

const ms = new MockService({
  // Define a base path to serve docs,
  // docs will be available at /api/mock-service/docs
  basePath: '/api/mock-service',

  redis: { host: config.redis.host, port: config.redis.port },

  // Optional, the "history" feature will use it to aggregate all requests by this predicate.
  // It could be a request ID, generated by AsyncLocalStorage.
  requestAggKey: () => AppContext.getRequestId(),

  // Define a pattern to import files with default mocks.
  filesPattern: '**/modules/**/*.mocking*',

  // Blacklist annoying requests to prevent them from appearing in history.
  // You can also change it in real-time.
  reqBlacklist: [
    /sqs.*amazonaws.com/,
    /s3.*amazonaws.com/,
    /sns.*amazonaws.com/,
  ],
})

// It's possible to subscribe to various events.
ms.events.on('mock:set', ({ id, body }) =>
  logger.debug(`Mock set: ${id} with body:`, body),
)

// And more events:
ms.events.on('mock:drop', () => {})
ms.events.on('mock:drop-all', () => {})

ms.events.on('request:intercepted', () => {})
ms.events.on('request:match-custom-mock', () => {})
ms.events.on('request:match-custom-passthrough', () => {})
ms.events.on('request:match-default', () => {})
ms.events.on('request:passthrough', () => {})

ms.events.on('error', (err) => logger.error(err))

Define mocks

You can optionally define default mocks in your application. They are very similar to MSW mocks.

// github-api.mocking.ts
import { http, HttpResponse } from 'mocktory'

const api = http.setup('https://api.github.com')

// Note: `http.responseJSON` construction is prefferable here,
// since we can then easily show to QA/AQA user default response mock for better guide them.
api.get(`/repos/*`, http.responseJSON(null))

// Note: `http.responseJSON` supports templating from request body via {{requestBody}}.
// So you can still omit writing custom method
api.post(
  `/orgs/${config.orgName}/repos`,
  http.responseJSON({ name: '{{requestBody.name}}' }),
)

// Same generic type support as in MSW
api.post<{}, {}, {}>(
  `/repos/issues`,
  // You can still write custom response method, for end user it will be shown as "Custom response"
  async ({ request }) => {
    const body = await request.getRequest().json()

    // ...

    return HttpResponse.json({})
  },
)

Realtime mocks can be set via the API:

curl -X 'POST' \
  '<your project server url>/api/mock-service/mock/{feautureId}' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "pattern": "PASSTHROUGH"
}'

For more examples and patterns, see Swagger docs at /api/mock-service/docs.