Add cypress for e2e testing

This commit is contained in:
2024-10-14 19:45:10 +02:00
parent 95f92f1801
commit 872ede8b2a
11 changed files with 4857 additions and 105 deletions

View File

@@ -26,6 +26,26 @@ jobs:
npm ci
npm run build
- name: Install http-server
run: npm install --save-dev http-server
- name: Start HTTP server
run: npx http-server ./dist -p 8080 &
env:
CI: true
- name: Run Cypress tests and Browserslist
run: npm run test
env:
CYPRESS_baseUrl: http://localhost:8080
- name: Upload Cypress screenshots
if: failure()
uses: actions/upload-artifact@v4
with:
name: cypress-screenshots
path: cypress/screenshots
- name: Copy build directory to server
uses: Dylan700/sftp-upload-action@v1.2.3
with:

View File

@@ -26,6 +26,26 @@ jobs:
npm ci
npm run build
- name: Install http-server
run: npm install --save-dev http-server
- name: Start HTTP server
run: npx http-server ./dist -p 8080 &
env:
CI: true
- name: Run Cypress tests and Browserslist
run: npm run test
env:
CYPRESS_baseUrl: http://localhost:8080
- name: Upload Cypress screenshots
if: failure()
uses: actions/upload-artifact@v4
with:
name: cypress-screenshots
path: cypress/screenshots
- name: Copy build directory to server
uses: Dylan700/sftp-upload-action@v1.2.3
with:

13
cypress.config.ts Normal file
View File

@@ -0,0 +1,13 @@
import { defineConfig } from 'cypress';
import { addMatchImageSnapshotPlugin } from '@simonsmith/cypress-image-snapshot/plugin';
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
addMatchImageSnapshotPlugin(on);
},
specPattern: 'cypress/integration/**/*.js',
baseUrl: 'http://localhost:8080',
},
});

View File

@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}

View File

@@ -0,0 +1,18 @@
// cypress/integration/app_spec.js
describe('App Component', () => {
beforeEach(() => {
cy.visit('/');
});
it('renders the app and checks if CSS is loaded', () => {
cy.get('[data-testid="root"]', { timeout: 10000 }).should('exist');
cy.get('[data-testid="root"]').should('have.class', 'min-h-screen py-10 bg-gray-100 dark:bg-gray-900 m-0');
});
it('should match the previous screenshot', () => {
cy.get('[data-testid="root"]').matchImageSnapshot({
failureThreshold: 0.01, // tolérance de 1%
failureThresholdType: 'percent', // type de tolérance (peut être 'pixel' ou 'percent')
});
});
});

View File

@@ -0,0 +1,37 @@
/// <reference types="cypress" />
// ***********************************************
// This example commands.ts 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) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }

View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
</head>
<body>
<div data-cy-root></div>
</body>
</html>

View File

@@ -0,0 +1,39 @@
// ***********************************************************
// This example support/component.ts 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')
import { mount } from 'cypress/react18'
// Augment the Cypress namespace to include type definitions for
// your custom command.
// Alternatively, can be defined in cypress/support/component.d.ts
// with a <reference path="./component" /> at the top of your spec.
declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount
}
}
}
Cypress.Commands.add('mount', mount)
// Example use:
// cy.mount(<MyComponent />)

30
cypress/support/e2e.ts Normal file
View File

@@ -0,0 +1,30 @@
// ***********************************************************
// This example support/e2e.ts 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')
import {addMatchImageSnapshotCommand} from '@simonsmith/cypress-image-snapshot/command'
addMatchImageSnapshotCommand()
// can also add any default options to be used
// by all instances of `matchImageSnapshot`
addMatchImageSnapshotCommand({
failureThreshold: 0.2,
})

4745
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,9 +7,29 @@
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
"preview": "vite preview",
"cypress:open": "cypress open",
"cypress:run": "cypress run",
"test": "browserslist && cypress run"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"@testing-library/react": "^16.0.1",
"browserslist": "^4.24.0",
"browserslist-useragent": "^4.0.0",
"cypress": "^13.15.0",
"eslint-plugin-react": "^7.37.1",
"iconoir-react": "^7.9.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
@@ -17,6 +37,7 @@
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@simonsmith/cypress-image-snapshot": "^9.1.0",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",