Skip to content

Commit 890faf8

Browse files
authored
fix: use modern conventions for component testing setup (#1296)
* fix: use modern conventions for component testing setup * fix: fix yarn.lock
1 parent f7718b7 commit 890faf8

12 files changed

+56
-84
lines changed

cypress.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { percyHealthCheck } from "@percy/cypress/task";
77
import codeCoverageTask from "@cypress/code-coverage/task";
88
import { defineConfig } from "cypress";
99
import "@cypress/instrument-cra";
10-
const { devServer } = require("@cypress/react/plugins/react-scripts");
1110

1211
dotenv.config({ path: ".env.local" });
1312
dotenv.config();
@@ -52,7 +51,10 @@ module.exports = defineConfig({
5251
googleClientSecret: process.env.REACT_APP_GOOGLE_CLIENT_SECRET,
5352
},
5453
component: {
55-
devServer,
54+
devServer: {
55+
framework: "create-react-app",
56+
bundler: "webpack",
57+
},
5658
specPattern: "src/**/*.cy.{js,jsx,ts,tsx}",
5759
supportFile: "cypress/support/component.ts",
5860
setupNodeEvents(on, config) {

cypress.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { mount } from "cypress/react";
2+
3+
// Augment the Cypress namespace to include type definitions for
4+
// your custom command.
5+
// Alternatively, can be defined in cypress/support/component.d.ts
6+
// with a <reference path="./component" /> at the top of your spec.
7+
declare global {
8+
namespace Cypress {
9+
interface Chainable {
10+
mount: typeof mount;
11+
}
12+
}
13+
}

cypress/support/component.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
import "@cypress/code-coverage/support";
2+
// ***********************************************************
3+
// This example support/component.ts is processed and
4+
// loaded automatically before your test files.
5+
//
6+
// This is a great place to put global configuration and
7+
// behavior that modifies Cypress.
8+
//
9+
// You can change the location of this file or turn off
10+
// automatically serving support files with the
11+
// 'supportFile' configuration option.
12+
//
13+
// You can read more here:
14+
// https://on.cypress.io/configuration
15+
// ***********************************************************
16+
17+
// Import commands.js using ES2015 syntax:
218
import "./commands";
19+
20+
// Alternatively you can use CommonJS syntax:
21+
// require('./commands')
22+
23+
import { mount } from "cypress/react";
24+
25+
Cypress.Commands.add("mount", mount);
26+
27+
// Example use:
28+
// cy.mount(<MyComponent />)

cypress/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "../tsconfig.json",
3-
"include": ["./**/*.ts"],
3+
"include": ["./**/*.ts", "../cypress.d.ts"],
44
"exclude": [],
55
"compilerOptions": {
66
"types": ["cypress", "@percy/cypress"],

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"devDependencies": {
5252
"@cypress/code-coverage": "^3.10.0-dev.1",
5353
"@cypress/instrument-cra": "1.4.0",
54-
"@cypress/react": "^5.10.0",
5554
"@cypress/webpack-dev-server": "^1.6.0",
5655
"@faker-js/faker": "6.1.2",
5756
"@percy/cypress": "2.3.4",

src/components/AlertBar.cy.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { startCase } from "lodash";
2-
import { mount } from "@cypress/react";
32
import { interpret } from "xstate";
43
import AlertBar from "./AlertBar";
54
import { Severities, snackbarMachine } from "../machines/snackbarMachine";
@@ -20,7 +19,7 @@ describe("Alert Bar with state", () => {
2019
snackbarService.send(payload);
2120
expect(snackbarService.state.value).to.equal("visible");
2221

23-
mount(<AlertBar snackbarService={snackbarService} />);
22+
cy.mount(<AlertBar snackbarService={snackbarService} />);
2423
cy.get("[data-test*=alert-bar]")
2524
.should("contain", payload.message)
2625
.and("have.class", `MuiAlert-filled${startCase(severity)}`);

src/components/SignInForm.cy.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { mount } from "@cypress/react";
21
import { interpret } from "xstate";
32
import { MemoryRouter } from "react-router-dom";
43
import SignInForm from "./SignInForm";
@@ -31,7 +30,7 @@ describe("SignInForm", () => {
3130
});
3231

3332
it("submits the username and password to the backend", () => {
34-
mount(
33+
cy.mount(
3534
<MemoryRouter>
3635
<SignInForm authService={authService} />
3736
</MemoryRouter>

src/components/TransactionDateRangeFilter.cy.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from "react";
2-
import { mount } from "@cypress/react";
32
import { addDays, format as formatDate, startOfDay } from "date-fns";
43
import TransactionDateRangeFilter from "./TransactionDateRangeFilter";
54
import { endOfDayUTC } from "../utils/transactionUtils";
@@ -9,7 +8,7 @@ describe("Transaction Date Range Filter", () => {
98
const filterDateRangeSpy = cy.spy();
109
const resetDateRangeSpy = cy.spy();
1110

12-
mount(
11+
cy.mount(
1312
<TransactionDateRangeFilter
1413
filterDateRange={filterDateRangeSpy}
1514
dateRangeFilters={{}}
@@ -27,7 +26,7 @@ describe("Transaction Date Range Filter", () => {
2726
dateRangeEnd: new Date("Dec 05 2030").toISOString(),
2827
};
2928

30-
mount(
29+
cy.mount(
3130
<TransactionDateRangeFilter
3231
filterDateRange={filterDateRangeSpy}
3332
dateRangeFilters={dateRangeFilters}
@@ -45,7 +44,7 @@ describe("Transaction Date Range Filter", () => {
4544
const dateRangeStart = startOfDay(new Date(2014, 1, 1));
4645
const dateRangeEnd = endOfDayUTC(addDays(dateRangeStart, 1));
4746

48-
mount(
47+
cy.mount(
4948
<TransactionDateRangeFilter
5049
filterDateRange={filterDateRangeSpy}
5150
dateRangeFilters={{}}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as React from "react";
2-
import { mount } from "@cypress/react";
32
import TransactionTitle from "./TransactionTitle";
43

54
it("Transaction Title", () => {
65
cy.fixture("public-transactions.json").then((transactions) => {
76
const transaction = transactions.results[0];
8-
mount(<TransactionTitle transaction={transaction} />);
7+
cy.mount(<TransactionTitle transaction={transaction} />);
98
cy.get("[data-test*=transaction-sender]").should("contain", transaction.senderName);
109
});
1110
});

src/containers/TransactionsContainer.cy.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { mount } from "@cypress/react";
21
import { MemoryRouter } from "react-router-dom";
32
import TransactionsContainer from "./TransactionsContainer";
43

54
describe("Transactions Container", () => {
65
it("should not render transactions", () => {
7-
mount(
6+
cy.mount(
87
<MemoryRouter initialEntries={["/"]}>
98
<TransactionsContainer />
109
</MemoryRouter>
@@ -15,7 +14,7 @@ describe("Transactions Container", () => {
1514
cy.intercept("http://localhost:3001/transactions/*", {
1615
fixture: "public-transactions.json",
1716
});
18-
mount(
17+
cy.mount(
1918
<MemoryRouter initialEntries={["/"]}>
2019
<TransactionsContainer />
2120
</MemoryRouter>
@@ -27,7 +26,7 @@ describe("Transactions Container", () => {
2726
cy.intercept("http://localhost:3001/transactions/*", {
2827
fixture: "public-transactions.json",
2928
});
30-
mount(
29+
cy.mount(
3130
<MemoryRouter initialEntries={["/contacts"]}>
3231
<TransactionsContainer />
3332
</MemoryRouter>
@@ -39,7 +38,7 @@ describe("Transactions Container", () => {
3938
cy.intercept("http://localhost:3001/transactions/*", {
4039
fixture: "public-transactions.json",
4140
});
42-
mount(
41+
cy.mount(
4342
<MemoryRouter initialEntries={["/personal"]}>
4443
<TransactionsContainer />
4544
</MemoryRouter>

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
"noFallthroughCasesInSwitch": true,
1919
"types": ["cypress"]
2020
},
21-
"include": ["src/**/*.cy.{js,ts,jsx,tsx}", "scripts", "backend", "src/__tests__"]
21+
"include": ["src/**/*.cy.{js,ts,jsx,tsx}", "scripts", "backend", "src/__tests__", "cypress.d.ts"]
2222
}

yarn.lock

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,21 +3277,6 @@
32773277
debug "4.2.0"
32783278
find-yarn-workspace-root "^2.0.0"
32793279

3280-
"@cypress/[email protected]":
3281-
version "1.0.2"
3282-
resolved "https://registry.yarnpkg.com/@cypress/mount-utils/-/mount-utils-1.0.2.tgz#afbc4f8c350b7cd86edc5ad0db0cbe1e0181edc8"
3283-
integrity sha512-Fn3fdTiyayHoy8Ol0RSu4MlBH2maQ2ZEXeEVKl/zHHXEQpld5HX3vdNLhK5YLij8cLynA4DxOT/nO9iEnIiOXw==
3284-
3285-
"@cypress/react@^5.10.0":
3286-
version "5.12.0"
3287-
resolved "https://registry.yarnpkg.com/@cypress/react/-/react-5.12.0.tgz#f68e3e08bdbf6644f81246e0ca6ba9d8bbd6fb8d"
3288-
integrity sha512-SfkXf9Mg03gsOziOTo8oVhhwnEPF/3Vw13t9wE2crmRZsbLg1IaFKYDFKIvcMq821TDWkiU+xfjRZsOTQ05+LA==
3289-
dependencies:
3290-
"@cypress/mount-utils" "1.0.2"
3291-
debug "^4.3.2"
3292-
find-webpack "2.2.1"
3293-
find-yarn-workspace-root "2.0.0"
3294-
32953280
"@cypress/request@^2.88.10":
32963281
version "2.88.10"
32973282
resolved "https://registry.yarnpkg.com/@cypress/request/-/request-2.88.10.tgz#b66d76b07f860d3a4b8d7a0604d020c662752cce"
@@ -7567,13 +7552,6 @@ debug@4, [email protected], debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, de
75677552
dependencies:
75687553
ms "2.1.2"
75697554

7570-
7571-
version "4.1.1"
7572-
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
7573-
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
7574-
dependencies:
7575-
ms "^2.1.1"
7576-
75777555
75787556
version "4.2.0"
75797557
resolved "https://registry.yarnpkg.com/debug/-/debug-4.2.0.tgz#7f150f93920e94c58f5574c2fd01a3110effe7f1"
@@ -9095,24 +9073,7 @@ find-up@^3.0.0:
90959073
dependencies:
90969074
locate-path "^3.0.0"
90979075

9098-
9099-
version "2.2.1"
9100-
resolved "https://registry.yarnpkg.com/find-webpack/-/find-webpack-2.2.1.tgz#96e7b701a2d37c3500cae30d4dc59e14923ba460"
9101-
integrity sha512-OdDtn2AzQvu3l9U1TS5ALc7uTVcLK/yv3fhjo+Pz7yuv4hG3ANKnbkKnPIPZ5ofd9mpYe6wRf5g5H4X9Lx48vQ==
9102-
dependencies:
9103-
debug "4.1.1"
9104-
find-yarn-workspace-root "1.2.1"
9105-
mocked-env "1.3.2"
9106-
9107-
9108-
version "1.2.1"
9109-
resolved "https://registry.yarnpkg.com/find-yarn-workspace-root/-/find-yarn-workspace-root-1.2.1.tgz#40eb8e6e7c2502ddfaa2577c176f221422f860db"
9110-
integrity sha512-dVtfb0WuQG+8Ag2uWkbG79hOUzEsRrhBzgfn86g2sJPkzmcpGdghbNTfUKGTxymFrY/tLIodDzLoW9nOJ4FY8Q==
9111-
dependencies:
9112-
fs-extra "^4.0.3"
9113-
micromatch "^3.1.4"
9114-
9115-
[email protected], find-yarn-workspace-root@^2.0.0:
9076+
find-yarn-workspace-root@^2.0.0:
91169077
version "2.0.0"
91179078
resolved "https://registry.yarnpkg.com/find-yarn-workspace-root/-/find-yarn-workspace-root-2.0.0.tgz#f47fb8d239c900eb78179aa81b66673eac88f7bd"
91189079
integrity sha512-1IMnbjt4KzsQfnhnzNd8wUEgXZ44IzZaZmnLYx7D5FZlaHt2gW20Cri8Q+E/t5tIj4+epTBub+2Zxu/vNILzqQ==
@@ -9272,15 +9233,6 @@ fs-constants@^1.0.0:
92729233
resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
92739234
integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==
92749235

9275-
fs-extra@^4.0.3:
9276-
version "4.0.3"
9277-
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94"
9278-
integrity sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==
9279-
dependencies:
9280-
graceful-fs "^4.1.2"
9281-
jsonfile "^4.0.0"
9282-
universalify "^0.1.0"
9283-
92849236
fs-extra@^7.0.0:
92859237
version "7.0.1"
92869238
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
@@ -12380,16 +12332,6 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
1238012332
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
1238112333
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
1238212334

12383-
12384-
version "1.3.2"
12385-
resolved "https://registry.yarnpkg.com/mocked-env/-/mocked-env-1.3.2.tgz#548eb2fde141d083de70dc6b231cd9f3210d8731"
12386-
integrity sha512-jwm3ziowCjpbLNhUNYwn2G0tawV/ZGRuWeEGt6PItrkQT74Nk3pDldL2pmwm9sQZw6a/x+ZBGeBVYq54acTauQ==
12387-
dependencies:
12388-
check-more-types "2.24.0"
12389-
debug "4.1.1"
12390-
lazy-ass "1.6.0"
12391-
ramda "0.26.1"
12392-
1239312335
1239412336
version "1.10.0"
1239512337
resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7"
@@ -14478,11 +14420,6 @@ raf@^3.4.1:
1447814420
dependencies:
1447914421
performance-now "^2.1.0"
1448014422

14481-
14482-
version "0.26.1"
14483-
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.26.1.tgz#8d41351eb8111c55353617fc3bbffad8e4d35d06"
14484-
integrity sha512-hLWjpy7EnsDBb0p+Z3B7rPi3GDeRG5ZtiI33kJhTt+ORCd38AbAIjB/9zRIUoeTbE/AVX5ZkU7m6bznsvrf8eQ==
14485-
1448614423
random-bytes@~1.0.0:
1448714424
version "1.0.0"
1448814425
resolved "https://registry.yarnpkg.com/random-bytes/-/random-bytes-1.0.0.tgz#4f68a1dc0ae58bd3fb95848c30324db75d64360b"

0 commit comments

Comments
 (0)