Skip to content

Commit 79036b3

Browse files
nodebotanistLekoArts
andauthoredJul 25, 2022
chore(docs): Upgrade Cypress to v10 in E2E Testing (#36204)
* Upgraded Cypress to Version 10 (fixes #36097) * Added chonges to example * we -> you * Update end-to-end-testing.md * fix example Co-authored-by: Lennart <lekoarts@gmail.com>

18 files changed

+51
-238
lines changed
 

‎docs/docs/how-to/testing/end-to-end-testing.md

+22-23
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ To run Gatsby's development server and Cypress at the same time, you'll use the
1313
npm install --save-dev cypress start-server-and-test
1414
```
1515

16-
We also want the URLs used by `cy.visit()` or `cy.request()` to be prefixed, hence you have to create the file `cypress.json` at the root of your project with the following content:
16+
You need to create a config file for Cypress at the root of the project called `cypress.config.js`. You can use it to preface the URLs used by `cy.visit()` or `cy.request` as well as set the folder the tests are in by adding the following:
1717

18-
```json:title=cypress.json
19-
{
20-
"baseUrl": "http://localhost:8000/"
21-
}
18+
```js:title=cypress.config.js
19+
const { defineConfig } = require('cypress')
20+
21+
module.exports = defineConfig({
22+
e2e: {
23+
baseUrl: 'http://localhost:8000',
24+
specPattern: "cypress/e2e"
25+
}
26+
})
2227
```
2328

24-
Last but not least you add additional scripts to your `package.json` to run Cypress:
29+
You also need to add additional scripts to your `package.json` to run Cypress:
2530

2631
```json:title=package.json
2732
{
@@ -37,7 +42,7 @@ Last but not least you add additional scripts to your `package.json` to run Cypr
3742

3843
Type `npm run test:e2e` in your command line and see Cypress running for the first time. A folder named `cypress` will be created at the root of your project and a new application window will pop up. [Cypress' getting started guide](https://docs.cypress.io/guides/getting-started/writing-your-first-test.html#) is a good start to learn how to write tests!
3944

40-
_Important note_: If you are running Gatsby with the `--https` flag, whether using your own or automatically generated certificates, you will also need to tell `start-server-and-test` to disable HTTPS certificate checks (otherwise it will wait forever and never actually launch Cypress. You do this by passing in an environmental variable: `START_SERVER_AND_TEST_INSECURE=1`. [start-server-and-test docs](https://github.com/bahmutov/start-server-and-test#disable-https-certificate-checks)).
45+
**Please note:** If you are running Gatsby with the `--https` flag, whether using your own or automatically generated certificates, you will also need to tell `start-server-and-test` to disable HTTPS certificate checks (otherwise it will wait forever and never actually launch Cypress. You do this by passing in an environmental variable: `START_SERVER_AND_TEST_INSECURE=1`. [start-server-and-test docs](https://github.com/bahmutov/start-server-and-test#disable-https-certificate-checks)).
4146

4247
This means your `test:e2e` script would look like this:
4348

@@ -75,26 +80,16 @@ To use cypress-axe, you have to install the `cypress-axe` and [axe-core](https:/
7580
npm install --save-dev cypress-axe axe-core @testing-library/cypress
7681
```
7782

78-
Then you add the `cypress-axe` and `@testing-library/cypress` commands in `cypress/support/index.js`:
83+
Then you add the `cypress-axe` and `@testing-library/cypress` commands in `cypress/support/e2e.js`:
7984

80-
```js:title=cypress/support/index.js
81-
import "./commands"
85+
```js:title=cypress/support/e2e.js
8286
//highlight-start
8387
import "cypress-axe"
8488
import "@testing-library/cypress/add-commands"
8589
//highlight-end
8690
```
8791

88-
Cypress will look for tests inside the `cypress/integration` folder, but you can change the default test folder if you want. Because you're writing end-to-end tests, create a new folder called `cypress/e2e`, and use it as your `integrationFolder` in your `cypress.json`:
89-
90-
```json:title=cypress.json
91-
{
92-
"baseUrl": "http://localhost:8000/",
93-
"integrationFolder": "cypress/e2e" // highlight-line
94-
}
95-
```
96-
97-
Create a new file inside `cypress/e2e` folder and name it `accessibility.test.js`.
92+
Create a folder inside the cypress folder and a new file inside `cypress/e2e` folder and name it `accessibility.test.js`.
9893

9994
You'll use the `beforeEach` hook to run some commands before each test. This is what they do:
10095

@@ -109,7 +104,8 @@ Finally, inside the first test, you'll use the `checkA11y` command from `cypress
109104

110105
describe("Accessibility tests", () => {
111106
beforeEach(() => {
112-
cy.visit("/").get("main").injectAxe()
107+
cy.visit("/").get("main")
108+
cy.injectAxe()
113109
})
114110
it("Has no detectable accessibility violations on load", () => {
115111
cy.checkA11y()
@@ -132,7 +128,8 @@ The following test is for the [gatsby-default-starter](https://github.com/gatsby
132128

133129
describe("Accessibility tests", () => {
134130
beforeEach(() => {
135-
cy.visit("/").get("main").injectAxe()
131+
cy.visit("/").get("main")
132+
cy.injectAxe()
136133
})
137134
it("Has no detectable accessibility violations on load", () => {
138135
cy.checkA11y()
@@ -156,11 +153,13 @@ You'll now write another test for the `gatsby-default-starter` homepage. In this
156153

157154
describe("Accessibility tests", () => {
158155
beforeEach(() => {
159-
cy.visit("/").get("main").injectAxe()
156+
cy.visit("/").get("main")
157+
cy.injectAxe()
160158
})
161159
it("Has no detectable accessibility violations on load", () => {
162160
cy.checkA11y()
163161
})
162+
164163
it("Navigates to page 2 and checks for accessibility violations", () => {
165164
cy.findByText(/go to page 2/i)
166165
.click()

‎examples/using-cypress/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Gatsby example site that shows how to write accessibility tests with Cypress and
77
- `npm install`
88
- `npm run test:e2e`
99

10-
See the [End-to-End Testing](https://www.gatsbyjs.com/docs/end-to-end-testing/) guide for a walkthrough and more details.
10+
See the [End-to-End Testing](https://www.gatsbyjs.com/docs/how-to/testing/end-to-end-testing/) guide for a walkthrough and more details.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { defineConfig } = require('cypress')
2+
3+
module.exports = defineConfig({
4+
e2e: {
5+
baseUrl: 'http://localhost:8000',
6+
specPattern: "cypress/e2e"
7+
}
8+
})

‎examples/using-cypress/cypress.json

-4
This file was deleted.

‎examples/using-cypress/cypress/e2e/accessibility.test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/// <reference types="Cypress" />
2-
31
describe("Accessibility tests", () => {
42
beforeEach(() => {
5-
cy.visit("/").get("main").injectAxe()
3+
cy.visit("/").get("main")
4+
cy.injectAxe()
65
})
76
it("Has no detectable accessibility violations on load", () => {
87
cy.checkA11y()

‎examples/using-cypress/cypress/plugins/index.js

-21
This file was deleted.

‎examples/using-cypress/gatsby-config.js

+1-27
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,5 @@ module.exports = {
44
description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
55
author: `@gatsbyjs`,
66
},
7-
plugins: [
8-
`gatsby-plugin-react-helmet`,
9-
{
10-
resolve: `gatsby-source-filesystem`,
11-
options: {
12-
name: `images`,
13-
path: `${__dirname}/src/images`,
14-
},
15-
},
16-
`gatsby-transformer-sharp`,
17-
`gatsby-plugin-sharp`,
18-
{
19-
resolve: `gatsby-plugin-manifest`,
20-
options: {
21-
name: `gatsby-starter-default`,
22-
short_name: `starter`,
23-
start_url: `/`,
24-
background_color: `#663399`,
25-
theme_color: `#663399`,
26-
display: `minimal-ui`,
27-
icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
28-
},
29-
},
30-
// this (optional) plugin enables Progressive Web App + Offline functionality
31-
// To learn more, visit: https://gatsby.dev/offline
32-
// `gatsby-plugin-offline`,
33-
],
7+
plugins: [],
348
}

‎examples/using-cypress/gatsby-ssr.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.onRenderBody = ({ setHtmlAttributes }) => {
2+
setHtmlAttributes({ lang: `en` })
3+
}

‎examples/using-cypress/package.json

+8-19
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
"scripts": {
88
"build": "gatsby build",
99
"develop": "gatsby develop",
10-
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
11-
"start": "npm run develop",
1210
"serve": "gatsby serve",
1311
"clean": "gatsby clean",
1412
"cy:open": "cypress open",
@@ -18,25 +16,16 @@
1816
},
1917
"dependencies": {
2018
"gatsby": "next",
21-
"gatsby-image": "next",
22-
"gatsby-plugin-manifest": "next",
23-
"gatsby-plugin-offline": "next",
24-
"gatsby-plugin-react-helmet": "next",
25-
"gatsby-plugin-sharp": "next",
26-
"gatsby-source-filesystem": "next",
27-
"gatsby-transformer-sharp": "next",
28-
"prop-types": "^15.7.2",
29-
"react": "^16.12.0",
30-
"react-dom": "^16.12.0",
31-
"react-helmet": "^5.2.1"
19+
"prop-types": "^15.8.1",
20+
"react": "^18.2.0",
21+
"react-dom": "^18.2.0"
3222
},
3323
"devDependencies": {
34-
"@testing-library/cypress": "^5.3.1",
35-
"axe-core": "^3.5.5",
36-
"cypress": "^4.12.1",
37-
"cypress-axe": "^0.8.1",
38-
"prettier": "2.1.1",
39-
"start-server-and-test": "^1.11.3"
24+
"@testing-library/cypress": "^8.0.3",
25+
"axe-core": "^4.4.3",
26+
"cypress": "^10.3.1",
27+
"cypress-axe": "^1.0.0",
28+
"start-server-and-test": "^1.14.0"
4029
},
4130
"keywords": [
4231
"gatsby"

‎examples/using-cypress/src/components/image.js

-32
This file was deleted.

‎examples/using-cypress/src/components/layout.js

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/**
2-
* Layout component that queries for data
3-
* with Gatsby's useStaticQuery component
4-
*
5-
* See: https://www.gatsbyjs.com/docs/use-static-query/
6-
*/
7-
81
import React from "react"
92
import PropTypes from "prop-types"
103
import { useStaticQuery, graphql } from "gatsby"

‎examples/using-cypress/src/components/seo.js

-88
This file was deleted.
Binary file not shown.
-20.7 KB
Binary file not shown.
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import React from "react"
2-
32
import Layout from "../components/layout"
4-
import SEO from "../components/seo"
53

64
const NotFoundPage = () => (
75
<Layout>
8-
<SEO title="404: Not found" />
96
<h1>NOT FOUND</h1>
107
<p>You just hit a route that doesn&#39;t exist... the sadness.</p>
118
</Layout>
129
)
1310

1411
export default NotFoundPage
12+
13+
export const Head = () => <title>404: Not found</title>
+2-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import React from "react"
22
import { Link } from "gatsby"
3-
43
import Layout from "../components/layout"
5-
import Image from "../components/image"
6-
import SEO from "../components/seo"
74

85
const IndexPage = () => (
96
<Layout>
10-
<SEO title="Home" />
117
<h1>Hi people</h1>
128
<p>Welcome to your new Gatsby site.</p>
139
<p>Now go build something great.</p>
14-
<div style={{ maxWidth: `300px`, marginBottom: `1.45rem` }}>
15-
<Image />
16-
</div>
1710
<Link to="/page-2/">Go to page 2</Link>
1811
</Layout>
1912
)
2013

2114
export default IndexPage
15+
16+
export const Head = () => <title>Home</title>
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import React from "react"
22
import { Link } from "gatsby"
3-
43
import Layout from "../components/layout"
5-
import SEO from "../components/seo"
64

75
const SecondPage = () => (
86
<Layout>
9-
<SEO title="Page two" />
107
<h1>Hi from the second page</h1>
118
<p>Welcome to page 2</p>
129
<Link to="/">Go back to the homepage</Link>
1310
</Layout>
1411
)
1512

1613
export default SecondPage
14+
15+
export const Head = () => <title>Page two</title>

0 commit comments

Comments
 (0)
Please sign in to comment.