Giter Site home page Giter Site logo

cypress-mock-ssr's Introduction

@cypress/mock-ssr

NOTE: This experimental package is in Developer Preview and is not yet published to npm

Install

$ npm install --dev https://github.com/cypress-io/cypress-mock-ssr#master
$ yarn add -D https://github.com/cypress-io/cypress-mock-ssr#master

Cypress Commands

The following Cypress commands are bundled in this module.

mockSSR

mockSSR sends a request to the middleware endpoint /__cypress_server_mock to set a mock for the given payload. Multiple calls to mockSSR can be made, each with distinct payloads.

it("validate server rendered content", () => {
  const joke = "Our wedding was so beautiful, even the cake was in tiers."
  cy.mockSSR({
    hostname: "https://icanhazdadjoke.com",
    method: "GET",
    path: "/",
    statusCode: 200,
    body: {
      id: "NmbFtH69hFd",
      joke,
      status: 200,
    },
  })

  cy.visit("/")
  cy.contains("[data-cy=post]", joke)
})

clearSSRMocks

NOTE: clearSSRMocks is called in global beforeEach and after hooks when it is required in cypress/support/index.js.

clearSSRMocks sends a request to the middleware endpoint /__cypress_clear_mocks to clear any mocks for the given test. It should be called in a lifecycle hook, such as beforeEach.

Usage

Require mockSSRCommands in the cypress/support/index.js in your project.

// cypress/support/index.js
require("./commands")
require("@cypress/mock-ssr/mockSSRCommands")

Middleware

This module exports connect middleware for use in Node.js servers to provide mock routes for mocking and clearing Server Side Rendered (SSR) content.

cypressMockMiddleware can be used in any connect based Node.js server including Express, Next.js, Nuxt.js.

const { cypressMockMiddleware } = require("@cypress/mock-ssr")

const server = express()

server.use(cypressMockMiddleware())

Express

cypressMockMiddleware() can be used with a Express server to add the mocking capabilities for server rendered content.

const express = require("express")
const { cypressMockMiddleware } = require("@cypress/mock-ssr")

const server = express()

server.use(cypressMockMiddleware())

server.get("*", (req, res) => {
  // ...
})

server.listen(port, (err) => {
  if (err) throw err
  console.log(`> Ready on http://localhost:${port}`)
})

Next.js

Note: The custom server recommended is for testing purposes only and uses the base custom server provided by the Next.js team.

cypressMockMiddleware() can be used with a custom Next.js server to add the mocking capabilities to a Next.js project

// testServer.js
const express = require("express")
const next = require("next")
const { cypressMockMiddleware } = require("@cypress/mock-ssr")

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== "production"
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  const server = express()

  server.use(cypressMockMiddleware())

  server.get("*", (req, res) => {
    return handle(req, res)
  })

  server.listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

It is recommended that this testServer.js be added as a separate npm script to be used during test runs.

// package.json
"scripts": {
  "build": "next build",
  "dev": "next dev",
  "nextTestServer": "node testServer.js"
}

Nuxt.js

For use with Nuxt.js, import cypressMockMiddleware and add it to the serverMiddleware array of middlewares.

// nuxt.config.js
const { cypressMockMiddleware } = require("@cypress/mock-ssr")

export default {
  serverMiddleware: [cypressMockMiddleware()],
  // ...
}

Credit

cypress-mock-ssr's People

Contributors

cowboy avatar wolodev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.