Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rckeller/cypress-react

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rckeller/cypress-react

Unofficial Cypress utilities for testing React applications

  • 0.0.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

@rckeller/cypress-react

Unofficial Cypress utilities for testing React applications. Typescript definitions are included.

npm i -D @rckeller/cypress-react

Configuring Unit Testing

You can register cy.mount by importing it, which will initialize loading and caching for React, ReactDOM and component styles between test runs.

// support/index.js
import '@rckeller/cypress-react/mount'

cy.mount performs a full mount of a given component on a page, and aliases it as @ComponentName unless otherwise specified. You are free to access its properties, state, and even invoke its own methods. Unlike Jest and Enzyme, which simulate a thin virtual DOM that lacks full support for event propagination and other functionality, these components will be mounted on a live page in isolation that more closely represents a real use case.

// in a test file
cy.mount(<HelloWorld />)

cy.get('@HelloWorld')
  .invoke('setState', { name: 'React' })
cy.get('@HelloWorld')
  .its('state')
  .should('deep.equal', { name: 'React' })

Optionally, you can register a superset of cy.get, which aliases components by their displayname and allows you to select them in several different ways like so:

// support/index.js
import '@rckeller/cypress-unfetch/mount/get'
//  now you have access to new component selectors

// in a test file
cy.mount(<HelloWorld />)

cy.get('@HelloWorld')
cy.get(<HelloWorld />)
cy.get(HelloWorld)

Note, components are mounted in a fresh DOM, without any styles loaded. If you want to load components with styles, define Cypress.Styles as a link tag with your styles inlines.

// inline styles
Cypress.Styles = `<style type="text/css">p { color: black; }</style>`
// or use a CDN
Cypress.Styles = `<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet">`

Using a CDN may violate the same-origin policy, in which case, you'd have to set chromeWebSecurity to false in cypress.json.

Further Reading

This module is based on the official cypress-react-unit-test recipe, and is merely an abstraction layer for that implemetation. The original repository contains many different examples of how to take advantage of Cypress for unit testing components.

Keywords

FAQs

Package last updated on 10 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc