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

@kiyoshiro/msw-object

v0.0.3

Published

> Reusable msw mock definition.

Downloads

2

Readme

msw-object

Reusable msw mock definition.

codecov

Usage

npm i -D @kiyoshiro/msw-object
import {
  defineBaseRestBuilder,
  createRestHandler,
  RestBuilder,
} from "@kiyoshiro/msw-object";
import { setupServer } from "msw";

const baseBuilder = defineBaseRestBuilder({
  basePath: "http://localhost:3000",
  // Set default delay as 500ms
  // It is difficult with original msw!!
  delay: 500,
});

// extends baseBuilder
const userShowBuilder: RestBuilder = {
  ...baseBuilder,
  path: "/users/:id",
  resolve: () => ({ id: 1, name: "user1", age: 10 }),
};

// extends userShowBuilder
const userListBuilder: RestBuilder = {
  ...userShowBuilder,
  path: "/users",
  resolve: () => [userShowBuilder.resolve()],
};

const builders = [userShowBuilder, userListBuilder];
setupServer(...builders.map(createRestHandler));
import { rest, setupServer } from "msw";

const userShowHandler = rest.get(
  "http://localhost:3000/users/:id",
  (req, res, ctx) => {
    return res(
      ctx.status(200),
      ctx.json({ id: 1, name: "user1", age: 10 }),
      ctx.delay(500)
    );
  }
);

// difficult to reuse userShowBuilder 😢
const userListHandler = rest.get(
  "http://localhost:3000/users",
  (req, res, ctx) => {
    return res(
      ctx.status(200),
      ctx.json([{ id: 1, name: "user1", age: 10 }]),
      ctx.delay(500)
    );
  }
);

setupServer(userShowHandler, userListHandler);

Features

TODO...

Recipes

Switch the response by request query

const userListBuilder: RestBuilder = {
  ...baseBuilder,
  resolver: (req) => {
    switch (req.url.searchParams.get("category")) {
      case "follower":
        return [
          /* follower user list */
        ];
      case "follow":
        return [
          /* follow user list */
        ];
    }
  },
};

Inject spy methods in test

test("user show page", () => {
  const requestUrlSpy = jest.fn();
  const handler = createRestHandler({
    path: "/users/:id",
    resolver: () => ({ id: 1, name: "user1", age: 10 }),
    // You can specify onRequest hook. It is fired before mock handler return a response.
    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    onRequest: (req) => {
      requestUrlSpy(req.url.toString());
    },
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  });
  server.use(handler);

  render(<UserShow />);

  expect(requestUrlSpy).toHaveBeenCalledWith("/users/1");
});

TODO