Skip to content

Commit

Permalink
Cypress v12 - update config and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koalamango committed Jan 10, 2023
1 parent 0bd0372 commit b3e0573
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 63 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Cypress Unit Test Starter

A small repository to demonstrate unit testing of Progress Bar component in Cypress. It's built with React in TypeScript and styling with styled-components. For more information see the following tutorial
A small repository to demonstrate unit testing of Progress Bar component in Cypress 12. It's built with React in TypeScript and styling with styled-components. For more information see the following tutorial

https://medium.com/@koalamango/cypress-unit-testing-react-components-with-typescript-77b38e5043b3


![Cypress Unit Test](assets/ProgressBar.png)


### Development

```bash
Expand All @@ -17,6 +15,10 @@ yarn start
# To build
yarn build

# To test
yarn test:unit
# To test component
yarn cypress:run

# To test e2e
yarn cypress:e2e

```
Binary file modified assets/ProgressBar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from "cypress";

export default defineConfig({
component: {
devServer: {
framework: "create-react-app",
bundler: "webpack",
},
},
e2e: {
baseUrl: "http://localhost:3000",
supportFile: false,
},
defaultCommandTimeout: 5000,
video: true,
});
13 changes: 13 additions & 0 deletions cypress.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { mount } from "cypress/react";

// 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;
}
}
}
13 changes: 0 additions & 13 deletions cypress.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/// <reference types="cypress" />
import React from "react";
import { mount } from "cypress-react-unit-test";
import ProgressBar from "../../src/ProgressBar";
import GlobalStyle from "../../src/globalStyle";
import GlobalStyle from "../../src/GlobalStyle";

describe("Progress Bar", () => {
describe("<ProgressBar />", () => {
const mockSteps = ["Step 1", "Step 2", "Step 3", "Step 4"];
it("renders steps", () => {
mount(
cy.mount(
<ProgressBar steps={mockSteps}>
<GlobalStyle />
</ProgressBar>
Expand All @@ -26,9 +23,8 @@ describe("Progress Bar", () => {
.and("have.css", "background-color", "rgb(255, 255, 255)")
.and("have.css", "border-color", "rgb(0, 182, 237)");
});

it("renders active steps", () => {
mount(
cy.mount(
<ProgressBar steps={mockSteps} current={3}>
<GlobalStyle />
</ProgressBar>
Expand Down
5 changes: 5 additions & 0 deletions cypress/e2e/Example.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("E2E Template Spec", () => {
it("runs", () => {
cy.visit("https://example.cypress.io");
});
});
10 changes: 0 additions & 10 deletions cypress/plugins/index.js

This file was deleted.

25 changes: 0 additions & 25 deletions cypress/support/commands.js

This file was deleted.

37 changes: 37 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -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>
// }
// }
// }
12 changes: 12 additions & 0 deletions cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -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>
31 changes: 31 additions & 0 deletions cypress/support/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ***********************************************************
// 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";

// Ensure global app styles are loaded:
// import "../../src/index.css";

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

// import { mount } from "cypress/react";
import { mount } from "cypress/react18";

Cypress.Commands.add("mount", mount);

// Example use:
// cy.mount(<MyComponent />)
2 changes: 0 additions & 2 deletions cypress/support/index.js

This file was deleted.

9 changes: 9 additions & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"],
"jsx": "preserve"
},
"include": ["**/*.ts", "../cypress.d.ts"]
}
Binary file added cypress/videos/ProgressBar.cy.tsx.mp4
Binary file not shown.
Binary file removed cypress/videos/ProgressBar.spec.tsx.mp4
Binary file not shown.

0 comments on commit b3e0573

Please sign in to comment.