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

@gait-tools/cypress-server-mock

v2.0.1

Published

mocking library for cypress

Downloads

1

Readme

Cypress Server Mock

A tool for full stack mocking in cypress tests

Installation

npm i @gait-tools/cypress-server-mock

Usage

The package comes in 2 parts:

  1. The cypress command
  2. The api route called to implement mocking on the server

To use the cypress command add the following to your support/commands.ts file:

import "@gait-tools/cypress-server-mock/commands";

Currently only Next is supported.

Add a capture all api route under /pages/api/mock/ e.g. /pages/api/mock/[...mock].ts.

Inside that file you can either export a pre-configured mock handler e.g.:

import { undiciHandler } from "@gait-tools/cypress-server-mock";
const opts = { 
   createApiPath: "string";
   restoreApiPath: "string";
   ...rest // opts specific to the custom handler - see typing for more details here
} 

export default undiciHandler(opts);

or build out your own handler:

import { createHandler } from "@gait-tools/cypress-server-mock";

export default createHandler({
   createHandler: async (mockConfig) => {
    // function to create your mocks
   },
    restoreHandler: async () => {
      // function to restore your mocks 
    },
    /** optional path your mocking create endpoint sits on.
     * defaults to `api/mock/create`
     */
    createApiPath: "string"; 
    /** optional path your mocking restore endpoint sits on.
     * defaults to `api/mock/restore`
     */
    restoreApiPath: "string";
});

You can then use the cypress server mock in your tests:

it('should give me fake content', () => {
  cy.mockServerUrl({
    apiPath: "http://localhost:3000/api/mock", // the path to your mocking endpoints
    basePath: "http://external-service.company.com", // the path you want to mock
    path: "/content",
    method: "GET",
    response: {
      statusCode: 200,
      data: { content: "fake content" },
    }
  })
  
  cy.visit('http://localhost:3000');
})

afterEach(() => {
  cy.restoreServerMocks();
})

Security

We are looking at implementing a default block such that these routes are not available in production builds, however we currently do not have that functionality. Ensure you only have these mocking routes for development/testing builds.

Limitations

  • Currently we only support NextJS routes
  • We only allow you to mock api requests at the moment, but do not allow you to verify that a route was called
  • These routes are currently available in production, we are looking to limit that