Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/jest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Install dependencies
run: npm install --legacy-peer-deps

- name: Run Jest tests
run: npm run jest --ci
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

1 change: 1 addition & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm run jest
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions __tests__/__snapshots__/snapshot.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`matches snapshot 1`] = `
<div>
<h1>
Counter
</h1>
<p
data-testid="count"
>
0
</p>
<button
onClick={[Function]}
>
Increment
</button>
<button
onClick={[Function]}
>
Decrement
</button>
</div>
`;
9 changes: 9 additions & 0 deletions __tests__/snapshot.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import '@testing-library/jest-dom';
import App from '../src/App';
import renderer from 'react-test-renderer';

test('matches snapshot', () => {
const tree = renderer.create(<App />).toJSON();
expect(tree).toMatchSnapshot();
});
27 changes: 27 additions & 0 deletions __tests__/unit.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import App from '../src/App';

test('renders the counter', () => {
render(<App />);
const headingElement = screen.getByText(/Counter/i);
expect(headingElement).toBeInTheDocument();
});

test('increments the counter', () => {
render(<App />);
const incrementButton = screen.getByText(/Increment/i);
const countElement = screen.getByTestId('count');

fireEvent.click(incrementButton);
expect(countElement).toHaveTextContent('1');
});
Comment on lines +11 to +18
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more stable element selector than getByTestId to ensure the test remains robust against UI changes.


test('decrements the counter', () => {
render(<App />);
const decrementButton = screen.getByText(/Decrement/i);
const countElement = screen.getByTestId('count');

fireEvent.click(decrementButton);
expect(countElement).toHaveTextContent('-1');
});
Comment on lines +20 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test logic for decrementing the counter is correct. Again, consider using a more stable element selector than getByTestId.

22 changes: 22 additions & 0 deletions __tests__/visual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import 'expect-puppeteer';

const { toMatchImageSnapshot } = require('jest-image-snapshot');
expect.extend({ toMatchImageSnapshot });

jest.setTimeout(30000);

describe('Visual Regression Tests', () => {
beforeAll(async () => {
await page.goto('http://localhost:3000');
await page.setViewport({ width: 1200, height: 800 });
}, 30000);

it('should visually match the snapshot', async () => {
const image = await page.screenshot();
expect(image).toMatchImageSnapshot({
failureThreshold: 0.01, // 1% threshold
failureThresholdType: 'percent', // Use percentage threshold
});
}, 30000);
});
7 changes: 7 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
presets: [
'@babel/preset-env',
'@babel/preset-react',
'@babel/preset-typescript',
],
};
11 changes: 11 additions & 0 deletions jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
launch: {
headless: true,
},
server: {
command: 'npm start',
port: 3000,
launchTimeout: 10000,
debug: true,
},
};
28 changes: 28 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
},
projects: [
{
displayName: 'snapshot',
preset: 'ts-jest',
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
testMatch: ['<rootDir>/__tests__/snapshot.test.tsx'],
},
{
displayName: 'unit',
preset: 'ts-jest',
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
testMatch: ['<rootDir>/__tests__/unit.test.tsx'],
},
{
displayName: 'visual',
preset: 'jest-puppeteer',
testMatch: ['<rootDir>/__tests__/visual.test.ts'],
},
],
};
Loading