Skip to content

Commit f6dff53

Browse files
authored
Set up Cypress and configure local test environment (#829)
* Testing : Intial cypress setup * Added test for card component * Added dummy e2e tests * setup cypress code coverage * Improved code coverage for card component to 100% * updated eslint & fixes error after merge conflict * cleanup(ci.yml): removed build job from ci.yml * Add test and coverage job
1 parent e4305fe commit f6dff53

File tree

15 files changed

+2204
-108
lines changed

15 files changed

+2204
-108
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
es2021: true,
44
node: true,
55
browser: true,
6+
"cypress/globals": true
67
},
78
settings: {
89
react: {
@@ -16,6 +17,7 @@ module.exports = {
1617
'plugin:react-hooks/recommended',
1718
'plugin:@next/next/recommended',
1819
'plugin:prettier/recommended',
20+
'plugin:cypress/recommended',
1921
],
2022
parser: '@typescript-eslint/parser',
2123
parserOptions: {
@@ -25,7 +27,7 @@ module.exports = {
2527
ecmaVersion: 12,
2628
sourceType: 'module',
2729
},
28-
plugins: ['react', '@typescript-eslint', 'prettier'],
30+
plugins: ['react', '@typescript-eslint', 'prettier', 'cypress'],
2931
rules: {
3032
'array-bracket-spacing': ['error', 'never'],
3133
'object-curly-spacing': ['error', 'always'],

.github/workflows/ci.yml

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ on:
33
pull_request:
44
types: [opened, reopened, synchronize]
55

6+
env:
7+
CODECOV_UNIQUE_NAME: CODECOV_UNIQUE_NAME-${{ github.run_id }}-${{ github.run_number }}
8+
69
jobs:
710
code-quality-checks:
811
name: Code Quality Checks
@@ -92,51 +95,35 @@ jobs:
9295
done
9396
exit 1
9497
95-
build:
96-
name: Build check
97-
needs: code-quality-checks
98+
testing-and-coverage:
99+
name: Testing and Coverage
100+
needs: [code-quality-checks]
98101
runs-on: ubuntu-latest
99102
steps:
100103
- name: Checkout repository
101104
uses: actions/checkout@v4
102105
with:
103106
submodules: recursive
104-
105-
- name: Get yarn cache directory path
106-
id: yarn-cache-dir-path
107-
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
108107

109-
- name: Cache Node dependencies
110-
uses: actions/cache@v4
111-
id: yarn-cache
112-
with:
113-
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
114-
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
115-
restore-keys: |
116-
${{ runner.os }}-yarn-
117-
118-
- if: ${{ steps.yarn-cache.outputs.cache-hit != 'true' }}
119-
name: List the state of node modules
120-
continue-on-error: true
121-
run: yarn list
122-
123-
- name: Cache Next Build
124-
uses: actions/cache@v4
125-
with:
126-
path: |
127-
${{ steps.yarn-cache-dir-path.outputs.dir }}
128-
${{ github.workspace }}/.next/cache
129-
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
130-
restore-keys: |
131-
${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-
132-
133108
- name: Setup Node.js
134109
uses: actions/setup-node@v4
135110
with:
136111
node-version: 20
112+
cache: 'yarn'
137113

138114
- name: Install dependencies
139115
run: yarn install --frozen-lockfile
140116

141-
- name: Build
142-
run: yarn run build
117+
- name: Run development server
118+
run: yarn run dev &
119+
120+
- name: Run tests and generate coverage report
121+
run: yarn run test:coverage:all
122+
123+
- name: Upload coverage report to Codecov
124+
uses: codecov/codecov-action@v2
125+
with:
126+
token: ${{ secrets.CODECOV_TOKEN }}
127+
name: ${{ env.CODECOV_UNIQUE_NAME }}
128+
verbose: true
129+
fail_ci_if_error: true

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
# testing
1212
/coverage
13+
/.nyc_output
14+
coverage.json
1315

1416
# next.js
1517
/.next/

components/Card.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Link from 'next/link';
33
import TextTruncate from 'react-text-truncate';
4-
interface CardProps {
4+
export interface CardProps {
55
title: string;
66
body: string;
77
icon?: string;
@@ -35,29 +35,41 @@ const CardBody = ({
3535
return (
3636
<div className='group relative h-full w-full rounded-lg border border-gray-200 bg-white p-6 px-12 shadow-3xl dark:shadow-2xl dark:shadow-slate-900 transition-colors ease-in-out hover:bg-slate-100 dark:bg-slate-800 hover:dark:bg-slate-900/30'>
3737
<div className='flex justify-center '>
38-
{image && <img src={image} className='h-32 p-2' />}
38+
{image && (
39+
<img src={image} className='h-32 p-2' data-test='card-image' />
40+
)}
3941
</div>
4042
<div className='flex flex-row items-start mb-6'>
4143
{icon && (
4244
<span className='mr-6 flex h-14 w-14 flex-shrink-0 items-center justify-center rounded-lg border bg-blue-200 px-3 text-gray-900 dark:text-white'>
43-
<img src={icon} alt={title} className='h-full w-full' />
45+
<img
46+
src={icon}
47+
alt={title}
48+
className='h-full w-full'
49+
data-test='card-icon'
50+
/>
4451
</span>
4552
)}
4653
<p
4754
className={`mb-1 mt-1 items-center font-bold text-gray-900 dark:text-white ${headerSizeClasses[headerSize || 'medium']}`}
55+
data-test='card-title'
4856
>
4957
{title}
5058
</p>
5159
</div>
5260
<hr className='mb-4 mt-3.5 h-px border-0 bg-gray-400' />
5361
<p
5462
className={`mb-8 text-black mt-5 dark:text-white ${bodyTextSizeClasses[bodyTextSize || 'medium']} `}
63+
data-test='card-body'
5564
>
5665
{extended && <span dangerouslySetInnerHTML={{ __html: body }} />}
5766
{!extended && <TextTruncate element='span' line={3} text={body} />}
5867
</p>
5968
{link && (
60-
<p className='absolute bottom-3 right-5 font-medium opacity-0 transition-opacity delay-150 ease-in-out group-hover:opacity-100 text-black dark:text-white '>
69+
<p
70+
className='absolute bottom-3 right-5 font-medium opacity-0 transition-opacity delay-150 ease-in-out group-hover:opacity-100 text-black dark:text-white'
71+
data-test='card-read-more'
72+
>
6173
Read More
6274
</p>
6375
)}
@@ -78,7 +90,7 @@ const Card: React.FC<CardProps> = ({
7890
return (
7991
<>
8092
{link ? (
81-
<Link href={link}>
93+
<Link href={link} data-test='card-link'>
8294
<CardBody
8395
{...{
8496
title,

cypress.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from 'cypress';
2+
3+
export default defineConfig({
4+
component: {
5+
devServer: {
6+
framework: 'next',
7+
bundler: 'webpack',
8+
},
9+
specPattern: 'cypress/components/**/*.cy.{js,jsx,ts,tsx}',
10+
setupNodeEvents(on, config) {
11+
// eslint-disable-next-line @typescript-eslint/no-var-requires
12+
require('@cypress/code-coverage/task')(on, config);
13+
return config;
14+
},
15+
},
16+
e2e: {
17+
setupNodeEvents(on, config) {
18+
// eslint-disable-next-line @typescript-eslint/no-var-requires
19+
require('@cypress/code-coverage/task')(on, config);
20+
return config;
21+
},
22+
},
23+
});

cypress/components/Card.cy.tsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react';
2+
import Card from '../../components/Card';
3+
import { CardProps } from '../../components/Card';
4+
5+
const RoadmapProps: CardProps = {
6+
icon: '/icons/roadmap.svg',
7+
title: 'Roadmap',
8+
body: 'Explore our exciting plans and upcoming milestones. 🚀',
9+
headerSize: 'large',
10+
bodyTextSize: 'medium',
11+
link: 'https://github.com/orgs/json-schema-org/discussions/427',
12+
};
13+
14+
describe('Card Component', () => {
15+
// Render the Roadmap Card with default RoadmapProps
16+
it('should render Roadmap Card correctly', () => {
17+
cy.mount(<Card {...RoadmapProps} />);
18+
cy.get('[data-test="card-image"]').should('not.exist');
19+
cy.get('[data-test="card-icon"]').should(
20+
'have.attr',
21+
'src',
22+
RoadmapProps.icon,
23+
);
24+
cy.get('[data-test="card-title"]').should('have.text', RoadmapProps.title);
25+
cy.get('[data-test="card-body"]').should('have.text', RoadmapProps.body);
26+
cy.get('[data-test="card-read-more"]').should('have.text', 'Read More');
27+
cy.get('[data-test="card-link"]').should(
28+
'have.attr',
29+
'href',
30+
RoadmapProps.link,
31+
);
32+
cy.get('[data-test="card-title"]').should('have.class', 'text-[2rem]');
33+
cy.get('[data-test="card-body"]').should('have.class', 'text-[1rem]');
34+
});
35+
36+
// Render the Roadmap card with default header and body sizes
37+
it('should render Roadmap card with default header and body sizes', () => {
38+
const missingSizes = {
39+
...RoadmapProps,
40+
headerSize: undefined,
41+
bodyTextSize: undefined,
42+
};
43+
cy.mount(<Card {...missingSizes} />);
44+
cy.get('[data-test="card-title"]').should('have.class', 'text-[1.3rem]');
45+
cy.get('[data-test="card-body"]').should('have.class', 'text-[1rem]');
46+
});
47+
48+
// Render the Roadmap card with extended body text
49+
it('should render Roadmap card with extended body text', () => {
50+
const extendedBody = { ...RoadmapProps, extended: true };
51+
cy.mount(<Card {...extendedBody} />);
52+
cy.get('[data-test="card-body"]').find('span').should('exist');
53+
});
54+
55+
// Render the Roadmap card with image
56+
it('should render Roadmap card with image', () => {
57+
const imageProps = { ...RoadmapProps, image: '/icons/roadmap.svg' };
58+
cy.mount(<Card {...imageProps} />);
59+
cy.get('[data-test="card-image"]').should(
60+
'have.attr',
61+
'src',
62+
imageProps.image,
63+
);
64+
});
65+
66+
// Render the Roadmap Card with some missing props
67+
it('should render Roadmap card with some missing props correctly', () => {
68+
const missingProps = { ...RoadmapProps, icon: undefined, link: undefined };
69+
cy.mount(<Card {...missingProps} />);
70+
cy.get('[data-test="card-icon"]').should('not.exist');
71+
cy.get('[data-test="card-link"]').should('not.exist');
72+
});
73+
});

cypress/e2e/homepage.cy.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// describe('Homepage', () => {
2+
// it('should contains - Build more. Break less. Empower others.', () => {
3+
// cy.viewport(1280, 720);
4+
// cy.visit('http://localhost:3000');
5+
// cy.contains(/Build more. Break less. Empower others./i);
6+
// });
7+
// });

cypress/support/commands.ts

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+
// }

cypress/support/component-index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
7+
<title>Components App</title>
8+
<!-- Used by Next.js to inject CSS. -->
9+
<div id="__next_css__DO_NOT_USE__"></div>
10+
</head>
11+
<body>
12+
<div data-cy-root></div>
13+
</body>
14+
</html>

cypress/support/component.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ***********************************************************
2+
// This example support/component.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+
import '@cypress/code-coverage/support';
19+
import { mount } from 'cypress/react18';
20+
import '../../styles/globals.css';
21+
22+
// Augment the Cypress namespace to include type definitions for
23+
// your custom command.
24+
// Alternatively, can be defined in cypress/support/component.d.ts
25+
// with a <reference path="./component" /> at the top of your spec.
26+
27+
declare global {
28+
// eslint-disable-next-line @typescript-eslint/no-namespace
29+
namespace Cypress {
30+
interface Chainable {
31+
mount: typeof mount;
32+
}
33+
}
34+
}
35+
36+
Cypress.Commands.add('mount', mount);
37+
38+
// Example use:
39+
// cy.mount(<MyComponent />)

0 commit comments

Comments
 (0)