Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from cypress-io/vite-react
Browse files Browse the repository at this point in the history
[DX-160] Vite + React
  • Loading branch information
cowboy authored Aug 4, 2021
2 parents 0d34882 + 957c560 commit 2906224
Show file tree
Hide file tree
Showing 21 changed files with 2,705 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vite-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.DS_Store
dist
dist-ssr
*.local
36 changes: 36 additions & 0 deletions vite-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Component Testing Example: Vite + React

This is an example **Vite + React** project with Cypress component testing.

- `yarn` to install the dependencies
- `npx cypress open-ct` to run interactively
- `npx cypress run-ct` to run all tests headlessly

The following was done to create this example:

1. Initialize a baseline React project in the [vite-react](.) subdirectory
1. `yarn create vite vite-react --template react`
2. Commit [`5e0c93a`](https://github.com/cypress-io/cypress-component-testing-examples/commit/5e0c93a7df87ff05c8e9ddb422a423d0f504b0cb)
2. Install project dependencies
1. `cd vite-react`
2. `yarn`
3. Commit [`11df79d`](https://github.com/cypress-io/cypress-component-testing-examples/commit/11df79d9b1762dd38bd272b51332061a8c8102cc)
3. Refactor app into separate components, adjust global styles
1. Move some code from [src/App.jsx](src/App.jsx) into [src/Counter.jsx](src/Counter.jsx)
2. Move some styles from [src/App.css](src/App.css) into [src/Counter.css](src/Counter.css)
3. Move global app styles into [src/index.css](src/index.css)
4. Commit [`0ad82e9`](https://github.com/cypress-io/cypress-component-testing-examples/commit/0ad82e93b94deb803d82ddb4f6cb62052b2e3b0c)
4. Add Cypress component testing support with sample tests
1. `yarn add -D cypress @cypress/react @cypress/vite-dev-server`
2. Add [cypress.json](cypress.json)
3. Add [cypress/plugins/index.js](cypress/plugins/index.js)
4. Add [src/App.spec.ct.jsx](src/App.spec.ct.jsx), [src/Counter.spec.ct.jsx](src/Counter.spec.ct.jsx)
5. `npx cypress open-ct` (Notice that the background doesn't inherit global app styles)
6. Edit [cypress/support/index.js](cypress/support/index.js) to import global app styles, the Cypress test preview should update automatically
7. Commit [`63d75b8`](https://github.com/cypress-io/cypress-component-testing-examples/commit/63d75b889755c62cd3a5f7444416738f15f52ffe)

_This example was generated by [vite-react.sh](https://github.com/cypress-io/cypress-component-testing-examples/blob/scripts/vite-react.sh) on 2021-08-04. The original README follows._

---

(no original readme)
6 changes: 6 additions & 0 deletions vite-react/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"component": {
"testFiles": "**/*.spec.ct.{js,ts,jsx,tsx}",
"componentFolder": "src"
}
}
5 changes: 5 additions & 0 deletions vite-react/cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
15 changes: 15 additions & 0 deletions vite-react/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const path = require('path')
const { startDevServer } = require('@cypress/vite-dev-server')

module.exports = (on, config) => {
on('dev-server:start', (options) => {
return startDevServer({
options,
viteConfig: {
configFile: path.resolve(__dirname, '../../vite.config.js'),
},
})
})

return config
}
25 changes: 25 additions & 0 deletions vite-react/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
23 changes: 23 additions & 0 deletions vite-react/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')

// Ensure global app styles are loaded:
import '../../src/index.css'
13 changes: 13 additions & 0 deletions vite-react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions vite-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "vite-react",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
"devDependencies": {
"@cypress/react": "^5.9.3",
"@cypress/vite-dev-server": "^2.0.5",
"@vitejs/plugin-react-refresh": "^1.3.1",
"cypress": "^8.1.0",
"vite": "^2.4.4"
}
}
37 changes: 37 additions & 0 deletions vite-react/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
42 changes: 42 additions & 0 deletions vite-react/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react'
import logo from './logo.svg'
import { Counter } from './Counter'
import './App.css'

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>Hello Vite + React!</p>
<p>
<Counter />
</p>
<p>
Edit <code>App.jsx</code> and save to test HMR updates.
</p>
<p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
{' | '}
<a
className="App-link"
href="https://vitejs.dev/guide/features.html"
target="_blank"
rel="noopener noreferrer"
>
Vite Docs
</a>
</p>
</header>
</div>
)
}

export default App
8 changes: 8 additions & 0 deletions vite-react/src/App.spec.ct.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import { mount } from '@cypress/react'
import App from './App'

it('renders learn react link', () => {
mount(<App />)
cy.get('a').contains('Learn React')
})
3 changes: 3 additions & 0 deletions vite-react/src/Counter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
button {
font-size: calc(10px + 2vmin);
}
12 changes: 12 additions & 0 deletions vite-react/src/Counter.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { useState } from 'react'
import './Counter.css'

export const Counter = () => {
const [count, setCount] = useState(0)

return (
<button type="button" onClick={() => setCount((count) => count + 1)}>
count is: {count}
</button>
)
}
13 changes: 13 additions & 0 deletions vite-react/src/Counter.spec.ct.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { mount } from '@cypress/react'
import { Counter } from './Counter'

it('renders the counter', () => {
mount(<Counter />)
cy.get('button').contains('count is: 0')
})

it('increments counter when clicked', () => {
mount(<Counter />)
cy.get('button').click().contains('count is: 1')
})
15 changes: 15 additions & 0 deletions vite-react/src/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions vite-react/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
body {
margin: 0;
background-color: #282c34;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
7 changes: 7 additions & 0 deletions vite-react/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions vite-react/src/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
)
7 changes: 7 additions & 0 deletions vite-react/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()]
})
Loading

0 comments on commit 2906224

Please sign in to comment.