GithubHelp home page GithubHelp logo

felipelahti / cypress-react-component-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bahmutov/cypress-react-component-example

0.0 1.0 0.0 8.33 MB

Cypress React component testing example running on CI using Cypress GitHub Action

JavaScript 48.46% HTML 33.46% CSS 18.08%

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 felipelahti avatar renovate-bot avatar renovate[bot] avatar

Watchers

 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.