Giter VIP home page Giter VIP logo

cypress-react-component-example's Introduction

cypress-react-component-example

ci status badges status renovate-app badge cypress version

Run Cypress component tests using Cypress GitHub Action

Create a new project using Create React App

$ npx create-react-app cypress-react-component-example
$ cd cypress-react-component-example

Added Cypress and component testing dependencies

$ yarn add -D cypress \
  @cypress/react @cypress/webpack-dev-server webpack-dev-server

If we open Cypress once, it creates E2E tests

$ yarn cypress open

Add the component settings to the cypress.json file, pointing at the component tests that will be placed alongside the components in the src folder.

{
  "component": {
    "componentFolder": "src",
    "testFiles": "**/*spec.{js,jsx,ts,tsx}"
  }
}

Add the bundling setting for component specs in cypress/plugins/index.js file

// cypress/plugins/index.js
module.exports = (on, config) => {
  if (config.testingType === 'component') {
    require('@cypress/react/plugins/react-scripts')(on, config)
  }

  return config
}

Write the component test. When the project was created, it wrote src/App.test.js component test file to be executed using Jest. Let's recreate the same test in Cypress:

// src/App.spec.js
/* global cy */
import * as React from 'react'
import { mount } from '@cypress/react'
import App from './App'

it('renders learn react link', () => {
  mount(<App />)
  cy.contains(/learn react/i)
})

Launch the test runner with

$ yarn cypress open-ct

Click on the component spec file "App.spec.js" and enjoy ๐Ÿš€

App component test

Running on CI

Let's use Cypress GitHub Action to run all Cypress tests on CI. You can find the full workflow file in .github/workflows/ci.yml file. In summary, install and run E2E tests using the action, then call the action again, skipping the install and using a custom test command parameter:

- name: Run E2E tests ๐Ÿงช
  uses: cypress-io/github-action@v2

- name: Run Component tests ๐Ÿงช
  uses: cypress-io/github-action@v2
  with:
    # we have already installed everything
    install: false
    # to run component tests we need to use "cypress run-ct"
    command: yarn cypress run-ct

Component tests running on CI

Tips

If you see 'cy' is not defined no-undef linting error message, just add the global cy object comment to the spec file

// src/App.spec.js
/* global cy */

cypress-react-component-example's People

Contributors

bahmutov avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cypress-react-component-example's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/badges.yml
.github/workflows/ci.yml
  • cypress-io/github-action v2
  • cypress-io/github-action v2
npm
package.json
  • @testing-library/react 13.4.0
  • react 18.2.0
  • react-dom 18.2.0
  • react-scripts 5.0.1
  • @cypress/react 5.12.5
  • @cypress/webpack-dev-server 1.8.4
  • cypress 9.7.0
  • webpack-dev-server 4.11.1

  • Check this box to trigger a request for Renovate to run again on this repository

Update to working version of Component Testing?

Are there any plans to update this repository to use a GA version of Cypress Component Testing and to ensure that the workflow runs?

The workflow .github/workflows/ci.yml fails to run with the Component Test hanging. Presumably because the app is not compatible with the default Node.js 18.

Also, the E2E test emits the error message:

"Warning: The set-output command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/"

which leads to the GitHub information "We are monitoring telemetry for the usage of these commands and plan to fully disable them on 31st May 2023"


In terms of Cypress:

Cypress 11.0.0 released the first GA version of Component Testing. See Cypress Changelog 11.0.0, whereas this repo is using Cypress 9.7.0.

Also, the cypress-io/github-action added the component: parameter in v3.2.0.

.github/workflows/ci.yml still uses cypress-io/github-action@v2 where this parameter was not yet available.

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.