Skip to content

Instantly share code, notes, and snippets.

@basarat
Created May 7, 2020 05:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save basarat/df1f7a689139b6404deea49ed86bf95f to your computer and use it in GitHub Desktop.
Save basarat/df1f7a689139b6404deea49ed86bf95f to your computer and use it in GitHub Desktop.
// create react app
npx create-react-app my-app --template typescript
cd my-react-app
// install
npm i cypress cypress-react-unit-test
// tsconfig.json
"types": [
"cypress"
]
// package.json
Scripts
"cypress:open": "cypress open",
"cypress:run": "cypress run"
// cypress.json
{
"experimentalComponentTesting": true,
"componentFolder": "src/cypress/component",
"integrationFolder": "src/cypress/integration",
"supportFile": "src/cypress/support/index.js",
"pluginsFile": "src/cypress/plugins/index.js",
"fixturesFolder": false
}
// src/cypress/support/index.js
require('cypress-react-unit-test/support')
// src/cypress/plugins/index.js
module.exports = (on, config) => {
require('cypress-react-unit-test/plugins/react-scripts')(on, config)
return config
}
// src/cypress/component/App.test.tsx
import React from 'react';
import { mount } from 'cypress-react-unit-test’;
import App from '../../App';
describe('App', () => {
it('renders learn react link', () => {
mount(<App />);
// use standard Cypress commands
cy.contains('Learn React').should('be.visible');
});
});
// Run
npm run cypress:open
// See coverage
open coverage/lcov-report/index.html
// remove RTL (react testing library) from package.json
@nufaylr
Copy link

nufaylr commented Mar 5, 2021

Quick note cypress-react-unit-test has been renamed to @cypress/react.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment