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

@s-ui/mock

v1.6.0

Published

Mock provider

Downloads

6,918

Readme

sui-mock

It is mainly a wrapper around Mock Service Worker (MSW). It allows mocking by intercepting requests on the network level. Seamlessly reuse the same mock definition for testing, development, and debugging.

Installation

npm install @s-ui/mock --save-dev

Mockers

Request mocking with setupMocker

It returns all methods included in setupWorker and setupServer. It will work in browser and server sides.

👉 Check setupWorker and setupServer in MSW docs.

Usage

1. Create Request handlers

First of all we need to define the application request handlers. Request handler is a function that determines whether an outgoing request should be mocked, and specifies its mocked response.

Create a ./mocks folder in your project root and create a handlers.js file inside it.

This handlers.js should export an array of Request handlers.

Example: How to create a handler

In this case, given the request handler ([GET] /user) it specifies a response resolver mocked response (status 200 with body {name: 'John Doe'}).

// ./mocks/exampleGateway/user/handlers.js
import {rest} from 'msw'
import {apiUrl} from '../config.js'

const responseResolver = () => res(ctx.status(200), ctx.json({name: 'John Doe'}))

export const getUserHandler = rest.get(`${apiUrl}/user`, responseResolver)

Then, we export the list of handlers

// ./mocks/handlers.js
import {getUserHandler} from './mocks/exampleGateway/user/handlers.js'

export default [getUserHandler]

1.1 Autoload mocks folder.

Any file, under the folder routes will be autoloaded and you dont have to do anything to see your handler capturing request.

When you use the autoload feature, you have to create a folder structure following the same structure than your API request.

For example, to capture the request https://api.site.com/api/v1/user/123/settings, your folder structure in your project will be:

[root mono-repo]
 |
 - mocks
    |
    - routes
        |
        - api
            |
            - v1
                |
                - user
                    |
                    - :id
                        |
                        - settings
                            |
                            - index.js

Your index.js file has to expose a function for each http method that you want to capture:

export async function get({headers, body, params, query, cookies}) {
  return [200, {name: 'nombre'}]
}
export async function post({headers, body, params, query, cookies}) {
  return [200, {params}]
}
export async function put() {}
export async function del() {}

Every function have to return the status code of the request and the body.

2. Expose mocker from mocks folder

Once we have the handlers created, we will need to create a mocker with all the handlers already defined.

// ./mocks/index.js
import {setupMocker, rest} from '@s-ui/mock'
import applicationHandlers from './handlers.js'

const getMocker = (handlers = applicationHandlers) => setupMocker(handlers)
const getEmptyMocker = setupMocker

export {getEmptyMocker, getMocker, rest}

3. Use it everywhere

Use this mocker everywhere in your application, it means integration tests, e2e tests, component tests, etc. but also in your application code.

Example of mocking in browser:

// src/app.js
if (process.env.STAGE === 'development') {
  const mocker = await import('../mocks/index.js').then(pkg =>
    pkg.getMocker()
  )
  mocker.start({onUnhandledRequest: 'bypass'})
}

Example of mocking in server:

// src/hooks/preSSRHandler/index.js
if (process.env.STAGE === 'development') {
  const mocker = await import('../../../mocks/index.js').then(pkg =>
    pkg.getMocker()
  )

  mocker.start({onUnhandledRequest: 'bypass'})
}

Example of mocking in unit tests with all handlers:

// domain/test/example/exampleSpec.js
import axios from 'axios'
import {getMocker} from '../../../mocks/index.js'

describe('Example', () => {
  let mocker

  before(async () => {
    mocker = await getMocker()
    await mocker.start()
  })

  after(() => {
    mocker.stop()
  })

  it('should do something', async () => {
    const result = await axios.get('/user?id=1')
    expect(result).to.be.deep.equal({name: 'John Doe'})
  })

  it('should throw an error', async () => {
    // We also could define a handler for this case
    const getUserGenericErrorHandler = rest.get('/user', () => {
      const error = {
        errorMessage: `User '${username}' not found`,
      }

      return res(ctx.status(404), ctx.json(error))
    })

    mocker.use(getUserGenericErrorHandler)

    try {
      const result = await axios.get('/user?id=1')
    } catch(error) {
      expect(error).to.be.an.instanceof(Error)
    }

  })
})

Example of mocking in unit tests without default handlers:

// domain/test/example/exampleSpec.js
import axios from 'axios'
import {getEmptyMocker} from '../../../mocks/index.js'
import {getUserHandler} from '../../../mocks/userGateway/user/handlers.js'

describe('Example', () => {
  let mocker

  before(async () => {
    mocker = await getEmptyMocker()
    await mocker.start()
  })

  after(() => {
    mocker.stop()
  })

  // This is important to restore handlers after each test
  afterEach(() => {
    mocker.resetHandlers()
  })

  it('should do something', async () => {
    // IMPORTANT: Explicit define handlers for this test
    mocker.use(getUserHandler)

    const result = await axios.get('/user?id=1')
    expect(result).to.be.deep.equal({name: 'John Doe'})
  })
})

Example of mocking in E2E tests:

it("should get todo data - MSW will be overridden", () => {
  cy.window().then(window => {
    const { worker, rest } = window.msw;

    worker.use(
      rest.get(`${API_URL}todos/1`, (req, res, ctx) => {
        return res(
          ctx.status(200),
          ctx.json({
            id: 1,
            title: "Mocked by not MSW but Cypress",
            completed: true
          })
        );
      })
    );
  });
  // Assert that the title is not "Mocked by MSW"
  cy.contains("Mocked by not MSW but Cypress");
});

4. Make Cypress integration stable

When we run E2E test, if we start our mocker in application code it could cause a race condition and the mocker could be started before the E2E test.

To fix that, it is recommended to avoid starting the mocker in application code and start it in E2E environment.

Example of starting mocker in Cypress environment

// test-e2e/support/setup.js
import {getMocker, rest} from '../../mocks/index.js'

// cypress/supports/index.js
Cypress.on('test:before:run:async', async () => {
  if (window.msw) return

  const mocker = await getMocker()
  await mocker.start({onUnhandledRequest: 'bypass'})

  window.msw = {
    worker: mocker,
    rest
  }
})