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

hops-msw

v15.2.1

Published

Mock service worker support for Hops

Downloads

185

Readme

hops-msw

npm

Please see the main Hops Readme for general information and a Getting Started Guide.

This is a preset for Hops that can be used to set up a Mock Service Worker for your Hops app. It allows to mock REST and GraphQL APIs in unit and integration tests.

This package (together with jest-preset-hops) takes care of setting up the MSW mock server already, so if you use it for unit-testing, you do not need to do this yourself.

Installation

Add this preset to your existing Hops React project:

npm install --save hops-msw

In order to use it for unit-testing, you also need to install the jest-preset-hops package.

Note: Please be aware that only one service worker can exist per scope. Therefore hops-msw does not work together with hops-pwa.

Usage

Set the environment variable ENABLE_MSW to true when running your unit tests.

Example:

import { render, waitFor } from '@testing-library/react';
import { getMockServer } from 'hops-msw/unit';
import { graphql } from 'hops-msw';
import { withApolloTestProvider } from 'hops-react-apollo';

it('loads graphql data', async () => {
  getMockServer().use(
    graphql.query('search', (req, res, ctx) => {
      return res(
        ctx.data({
          __typename: 'Query',
          user: {
            __typename: 'User',
            name: 'Something',
          },
        })
      );
    })
  );

  const { getByText } = render(withApolloTestProvider(<HomeWithData />));

  await waitFor(() => getByText('Something'));
});

Read the MSW documentation for more information on how to write mocks.

See here an example of how to write an integration test using puppeteer.

Configuration

Preset Options

| Name | Type | Default | Required | Description | | --- | --- | --- | --- | --- | | mockServiceWorkerHandlersFile | String | '' | no | The path to your mock handlers file (which will be used during development) | | mockServiceWorkerUri | String | <basePath>/sw.js | no | The path on which the mock service worker will be served from |

mockServiceWorkerHandlersFile

Use this option to register an array of handlers. These will be used during development if ENABLE_MSW=true is set. See here for an example.

Note: Unfortunately the handlers must be written in ES5 (no import/export) until we can drop Node.js 12 support.

"hops": {
  "mockServiceWorkerHandlersFile": "<rootDir>/mocks.js"
}
mockServiceWorkerUri

Usually this doesn't need to be changed.