Skip to content

Commit aef66ae

Browse files
committed
test(react): added e2e test using cypress
1 parent 34eb6ae commit aef66ae

File tree

14 files changed

+1314
-57
lines changed

14 files changed

+1314
-57
lines changed

package-lock.json

Lines changed: 1137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"autoprefixer": "^10.4.19",
3939
"concurrently": "^8.2.2",
4040
"cross-fetch": "^4.0.0",
41+
"cypress": "^13.13.0",
4142
"eslint": "^8.57.0",
4243
"eslint-config-prettier": "^9.1.0",
4344
"eslint-plugin-prettier": "^5.1.3",

packages/react/cypress.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'cypress';
2+
3+
export default defineConfig({
4+
e2e: {
5+
baseUrl: 'http://localhost:8000'
6+
}
7+
});

packages/react/cypress/e2e/App.cy.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference types="cypress" />
2+
3+
import { GET_TODOS } from 'shared-code/tests/todos.fixtures';
4+
import { cypressIntercepts } from 'shared-code/tests/cypress.intercepts';
5+
6+
describe('App E2E tests', () => {
7+
before(() => {
8+
cypressIntercepts();
9+
// Visit homepage
10+
cy.visit('/');
11+
12+
cy.wait('@getTodos');
13+
});
14+
15+
it('user can see rendered todos', () => {
16+
for (const todo of GET_TODOS) {
17+
// Get the DOM element containing the text and assert that it is visible
18+
cy.contains(todo.description).should('be.visible');
19+
}
20+
});
21+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }

packages/react/cypress/support/e2e.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/e2e.ts is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands';
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

packages/react/cypress/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"isolatedModules": false,
5+
"types": ["cypress", "chai"]
6+
},
7+
"include": [
8+
"../cypress/**/*.ts",
9+
"../cypress.config.js",
10+
"../node_modules/cypress/**/*.ts"
11+
]
12+
}

packages/react/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"test": "vitest",
2020
"test:watch": "vitest --watch",
2121
"test:ui": "vitest --ui",
22-
"test:coverage": "vitest --coverage"
22+
"test:coverage": "vitest --coverage",
23+
"cy:open": "cypress open",
24+
"cy": "cypress open"
2325
},
2426
"dependencies": {
2527
"@fortawesome/react-fontawesome": "^0.2.2",

packages/react/tsconfig.app.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
55
"target": "ES2020",
66
"useDefineForClassFields": true,
7-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"lib": [
8+
"ES2020",
9+
"DOM",
10+
"DOM.Iterable"
11+
],
812
"module": "ESNext",
913
"skipLibCheck": true,
10-
1114
/* Bundler mode */
1215
"moduleResolution": "bundler",
1316
"allowImportingTsExtensions": true,
@@ -16,16 +19,19 @@
1619
"moduleDetection": "force",
1720
"noEmit": true,
1821
"jsx": "react-jsx",
19-
2022
/* Linting */
2123
"strict": true,
2224
"noUnusedLocals": true,
2325
"noUnusedParameters": true,
2426
"noFallthroughCasesInSwitch": true,
25-
2627
// Add types support for @testing-library/jest-dom asserts like .toBeInTheDocument()
2728
"types": ["@testing-library/jest-dom/vitest"]
2829
},
30+
"exclude": [
31+
"cypress/**/*.ts",
32+
"cypress.config.js",
33+
"node_modules/cypress*/*.ts"
34+
],
2935
"include": [
3036
"src"
3137
]

packages/react/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {defineConfig} from 'vite';
1+
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33

44
// https://vitejs.dev/config/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference types="cypress" />
2+
3+
import { GET_TODOS } from './todos.fixtures';
4+
5+
export const cypressIntercepts = () => {
6+
cy.intercept({ method: 'GET', url: 'http://localhost:3000/todos' }, GET_TODOS).as(
7+
'getTodos'
8+
);
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { http, HttpResponse } from 'msw';
2+
import { GET_TODOS } from './todos.fixtures';
3+
import { GetTodosResponse, PatchTodoResponse, CreateTodoResponse, DeleteTodoResponse } from '../models/Api';
4+
5+
// Simulates getting TODO from DB
6+
const findTodoById = (id: string) => GET_TODOS.find((todo) => todo.id === Number(id))!;
7+
8+
const API = 'http://localhost:3000/todos';
9+
10+
export const handlers = [
11+
// Getting TODOs
12+
http.get<never, never, GetTodosResponse>(API, () => HttpResponse.json(GET_TODOS)),
13+
14+
15+
// For creating TODO item
16+
http.post<never, { description: string }, CreateTodoResponse>(API, async ({
17+
request
18+
}) => {
19+
const { description } = await request.json();
20+
21+
return HttpResponse.json({
22+
id: 100,
23+
description,
24+
completed: false,
25+
created_at: new Date(),
26+
updated_at: new Date()
27+
});
28+
}),
29+
30+
// For completing TODOs
31+
http.patch<{ id: string }, { completed: boolean }, PatchTodoResponse>(`${API}/:id`, async ({
32+
request,
33+
params
34+
}) => {
35+
const data = await request.json();
36+
const { id } = params;
37+
const todo = findTodoById(id);
38+
return HttpResponse.json({ ...todo, ...data });
39+
}),
40+
41+
42+
// For completing TODOs
43+
http.delete<{ id: string }, never, DeleteTodoResponse>(`${API}/:id`, () => {
44+
// No logic for deletion is needed
45+
return HttpResponse.json(true);
46+
})
47+
] as const;

packages/shared-code/tests/server.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,4 @@
1-
import {http, HttpResponse} from "msw";
2-
import {setupServer} from "msw/node";
3-
import {GET_TODOS} from "./todos.fixtures";
4-
import {GetTodosResponse, PatchTodoResponse, CreateTodoResponse, DeleteTodoResponse} from '../models/Api';
1+
import { setupServer } from 'msw/node';
2+
import { handlers } from './msw.handlers';
53

6-
// Simulates getting TODO from DB
7-
const findTodoById = (id: string) => GET_TODOS.find((todo) => todo.id === Number(id))!;
8-
9-
export const todoServer = setupServer();
10-
11-
const API = 'http://localhost:3000/todos';
12-
13-
const handlers = [
14-
// Getting TODOs
15-
http.get<never, never, GetTodosResponse>(API, () => HttpResponse.json(GET_TODOS)),
16-
17-
18-
// For creating TODO item
19-
http.post<never, { description: string }, CreateTodoResponse>(API, async ({
20-
request,
21-
}) => {
22-
const {description} = await request.json();
23-
24-
return HttpResponse.json({
25-
id: 100,
26-
description,
27-
completed: false,
28-
created_at: new Date(),
29-
updated_at: new Date()
30-
})
31-
}),
32-
33-
// For completing TODOs
34-
http.patch<{ id: string }, { completed: boolean }, PatchTodoResponse>(`${API}/:id`, async ({
35-
request,
36-
params
37-
}) => {
38-
const data = await request.json();
39-
const {id} = params;
40-
const todo = findTodoById(id);
41-
return HttpResponse.json({...todo, ...data})
42-
}),
43-
44-
45-
// For completing TODOs
46-
http.delete<{ id: string }, never, DeleteTodoResponse>(`${API}/:id`, () => {
47-
// No logic for deletion is needed
48-
return HttpResponse.json(true)
49-
}),
50-
] as const;
51-
52-
todoServer.use(...handlers);
4+
export const todoServer = setupServer(...handlers);

packages/shared-code/tests/worker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { setupWorker } from 'msw/browser';
2+
import { handlers } from './msw.handlers';
3+
4+
// TODO: This could potentially work instead of duplicating works using Cypress intercepts - but it does not... :(
5+
// @see https://github.com/mswjs/msw/issues/744
6+
export const todoWorker = setupWorker(...handlers);

0 commit comments

Comments
 (0)