Skip to content

Commit 829bb3e

Browse files
authored
Introduce testing library with cypress (#1284)
Related to RaspberryPiFoundation/digital-editor-issues#878 We're already using https://github.com/testing-library for our react unit tests code, the library for cypress brings the same benefits. It will allow us to write end to end tests that better mimic user behaviour by interacting with elements based on their role/text/labels rather than their classes. As well as making the specs easier to follow and de-coupled from our class names, it helps to ensure our site is accessible (e.g. form elements have labels). To demonstrate this I've written added a new test to work with multiple files. If we like this approach, we can change over other specs as we write or update them. Note that cypress testing library doesn't seem to have direct support for the Shadow DOM yet (see testing-library/dom-testing-library#413 ) but we still works if we load the shadow DOM using the cypress finder. One quirk I found is that `findByLabelText` doesn't work from the root shadow DOM element which is why I've called it on `children()`. This branch is based off of `update-cypress`. If we're happy I will merge to main after that branch. --- Feedback wanted: - Do we think there's a benefit to using testing library for our cypress specs? - Are we happy to migrate them over gradually? Alternatively we could invest a bit of time to do them all at once (perhaps with the help of a coding agent).
1 parent b99cd5c commit 829bb3e

File tree

6 files changed

+92
-8
lines changed

6 files changed

+92
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
### Changed
1010

11+
- Introduce @testing-library for cypress
1112
- Update cypress (#1283)
1213

1314
### Fixed

cypress.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ dotenv.config();
66
export default defineConfig({
77
e2e: {
88
chromeWebSecurity: false,
9-
supportFile: false,
109
defaultCommandTimeout: 10000,
1110
video: false,
1211
defaultBrowser: "chrome",

cypress/e2e/spec-wc-pyodide.cy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("Running the code with pyodide", () => {
3434
configurable: true,
3535
});
3636
});
37+
cy.get("editor-wc").shadow().children().as("editor");
3738
});
3839

3940
it("runs a simple program", () => {
@@ -151,6 +152,35 @@ describe("Running the code with pyodide", () => {
151152
.should("contain", "3");
152153
});
153154

155+
it("runs a program with muiltiple files", () => {
156+
cy.get("@editor")
157+
.findByLabelText('editor text input')
158+
.invoke("text", `from my_number import NUMBER\nprint(NUMBER)\n`);
159+
160+
cy.get("@editor").findByRole('button', { name: 'Add file' }).click()
161+
162+
cy.get("@editor")
163+
.findByLabelText(/Name your file/)
164+
.type("my_number.py");
165+
166+
cy.get("@editor")
167+
.findByRole('dialog')
168+
.findByRole('button', { name: 'Add file' }).click()
169+
170+
cy.get("@editor")
171+
.findByLabelText('editor text input')
172+
.invoke("text", `NUMBER = 42\n`);
173+
174+
cy.get("@editor")
175+
.findByRole('button', { name: 'Run' }).click();
176+
177+
cy.get("@editor")
178+
.find(".pyodiderunner")
179+
.findByLabelText('Text output')
180+
.should("contain", "42");
181+
});
182+
183+
154184
it("runs a simple program with a built-in pyodide module", () => {
155185
runCode(
156186
"import simplejson as json\nprint(json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]))",

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/cypress/add-commands'

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"@pmmmwh/react-refresh-webpack-plugin": "0.4.3",
115115
"@react-three/test-renderer": "8.2.1",
116116
"@svgr/webpack": "5.5.0",
117+
"@testing-library/cypress": "^10.1.0",
117118
"@testing-library/jest-dom": "^5.16.5",
118119
"@testing-library/react": "14.3.1",
119120
"@testing-library/user-event": "^12.1.10",

yarn.lock

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,13 @@ __metadata:
16201620
languageName: node
16211621
linkType: hard
16221622

1623+
"@babel/runtime@npm:^7.14.6":
1624+
version: 7.28.4
1625+
resolution: "@babel/runtime@npm:7.28.4"
1626+
checksum: 934b0a0460f7d06637d93fcd1a44ac49adc33518d17253b5a0b55ff4cb90a45d8fe78bf034b448911dbec7aff2a90b918697559f78d21c99ff8dbadae9565b55
1627+
languageName: node
1628+
linkType: hard
1629+
16231630
"@babel/template@npm:^7.25.7, @babel/template@npm:^7.3.3":
16241631
version: 7.25.7
16251632
resolution: "@babel/template@npm:7.25.7"
@@ -2966,6 +2973,7 @@ __metadata:
29662973
"@sentry/tracing": 7.16.0
29672974
"@svgr/webpack": 5.5.0
29682975
"@szhsin/react-menu": ^3.2.0
2976+
"@testing-library/cypress": ^10.1.0
29692977
"@testing-library/jest-dom": ^5.16.5
29702978
"@testing-library/react": 14.3.1
29712979
"@testing-library/user-event": ^12.1.10
@@ -3707,6 +3715,34 @@ __metadata:
37073715
languageName: node
37083716
linkType: hard
37093717

3718+
"@testing-library/cypress@npm:^10.1.0":
3719+
version: 10.1.0
3720+
resolution: "@testing-library/cypress@npm:10.1.0"
3721+
dependencies:
3722+
"@babel/runtime": ^7.14.6
3723+
"@testing-library/dom": ^10.1.0
3724+
peerDependencies:
3725+
cypress: ^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0
3726+
checksum: 0a5a433fff11c35244d87f2a1d5974d5ef4478212a94245062b4dce5459bc367b26faa9f436cf11f2cb956927658b9849506735a93f64247fffc8690fc603fcb
3727+
languageName: node
3728+
linkType: hard
3729+
3730+
"@testing-library/dom@npm:^10.1.0":
3731+
version: 10.4.1
3732+
resolution: "@testing-library/dom@npm:10.4.1"
3733+
dependencies:
3734+
"@babel/code-frame": ^7.10.4
3735+
"@babel/runtime": ^7.12.5
3736+
"@types/aria-query": ^5.0.1
3737+
aria-query: 5.3.0
3738+
dom-accessibility-api: ^0.5.9
3739+
lz-string: ^1.5.0
3740+
picocolors: 1.1.1
3741+
pretty-format: ^27.0.2
3742+
checksum: 3887fe95594b6d9467a804e2cc82e719c57f4d55d7d9459b72a949b3a8189db40375b89034637326d4be559f115abc6b6bcfcc6fec0591c4a4d4cdde96751a6c
3743+
languageName: node
3744+
linkType: hard
3745+
37103746
"@testing-library/dom@npm:^9.0.0":
37113747
version: 9.3.4
37123748
resolution: "@testing-library/dom@npm:9.3.4"
@@ -5224,6 +5260,15 @@ __metadata:
52245260
languageName: node
52255261
linkType: hard
52265262

5263+
"aria-query@npm:5.3.0":
5264+
version: 5.3.0
5265+
resolution: "aria-query@npm:5.3.0"
5266+
dependencies:
5267+
dequal: ^2.0.3
5268+
checksum: 305bd73c76756117b59aba121d08f413c7ff5e80fa1b98e217a3443fcddb9a232ee790e24e432b59ae7625aebcf4c47cb01c2cac872994f0b426f5bdfcd96ba9
5269+
languageName: node
5270+
linkType: hard
5271+
52275272
"aria-query@npm:^5.0.0":
52285273
version: 5.3.2
52295274
resolution: "aria-query@npm:5.3.2"
@@ -8227,6 +8272,13 @@ __metadata:
82278272
languageName: node
82288273
linkType: hard
82298274

8275+
"dequal@npm:^2.0.3":
8276+
version: 2.0.3
8277+
resolution: "dequal@npm:2.0.3"
8278+
checksum: 8679b850e1a3d0ebbc46ee780d5df7b478c23f335887464023a631d1b9af051ad4a6595a44220f9ff8ff95a8ddccf019b5ad778a976fd7bbf77383d36f412f90
8279+
languageName: node
8280+
linkType: hard
8281+
82308282
"destroy@npm:1.2.0":
82318283
version: 1.2.0
82328284
resolution: "destroy@npm:1.2.0"
@@ -15500,20 +15552,20 @@ __metadata:
1550015552
languageName: node
1550115553
linkType: hard
1550215554

15555+
"picocolors@npm:1.1.1, picocolors@npm:^1.0.0, picocolors@npm:^1.1.0":
15556+
version: 1.1.1
15557+
resolution: "picocolors@npm:1.1.1"
15558+
checksum: e1cf46bf84886c79055fdfa9dcb3e4711ad259949e3565154b004b260cd356c5d54b31a1437ce9782624bf766272fe6b0154f5f0c744fb7af5d454d2b60db045
15559+
languageName: node
15560+
linkType: hard
15561+
1550315562
"picocolors@npm:^0.2.1":
1550415563
version: 0.2.1
1550515564
resolution: "picocolors@npm:0.2.1"
1550615565
checksum: 3b0f441f0062def0c0f39e87b898ae7461c3a16ffc9f974f320b44c799418cabff17780ee647fda42b856a1dc45897e2c62047e1b546d94d6d5c6962f45427b2
1550715566
languageName: node
1550815567
linkType: hard
1550915568

15510-
"picocolors@npm:^1.0.0, picocolors@npm:^1.1.0":
15511-
version: 1.1.1
15512-
resolution: "picocolors@npm:1.1.1"
15513-
checksum: e1cf46bf84886c79055fdfa9dcb3e4711ad259949e3565154b004b260cd356c5d54b31a1437ce9782624bf766272fe6b0154f5f0c744fb7af5d454d2b60db045
15514-
languageName: node
15515-
linkType: hard
15516-
1551715569
"picomatch@npm:^2.0.4, picomatch@npm:^2.2.1, picomatch@npm:^2.2.2, picomatch@npm:^2.2.3, picomatch@npm:^2.3.1":
1551815570
version: 2.3.1
1551915571
resolution: "picomatch@npm:2.3.1"

0 commit comments

Comments
 (0)