-
Notifications
You must be signed in to change notification settings - Fork 0
Test Jest Github Actions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryo-kozin
wants to merge
12
commits into
main
Choose a base branch
from
jest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e815830
Implement jest tests
ryo-kozin 25a409f
Add jest-puppeteer and ignore snapshots
ryo-kozin 268916d
Integrate husky
ryo-kozin 97dfc45
Create jest github action
ryo-kozin 664ad9d
Add --legacy-peer-deps flag
ryo-kozin 554db78
Store __snapshots__
ryo-kozin 9392357
Add start app to jest Github Action
ryo-kozin 676fb88
Update visual.test
ryo-kozin ec26b1f
Update image snapshots
ryo-kozin 89db060
Update visual snapshot tolerance for Jest tests
ryo-kozin a1b7313
Set jest vial snapshots timeout
ryo-kozin fa975fa
Set jest vial snapshots timeout globally
ryo-kozin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ | |
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| npm run jest |
Binary file added
BIN
+9.82 KB
...l-test-ts-visual-regression-tests-should-visually-match-the-snapshot-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }); | ||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'], | ||
| }, | ||
| ], | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
getByTestIdto ensure the test remains robust against UI changes.