Skip to content

Commit c652b7e

Browse files
authored
Setting up unit testing: Add Jest configuration, tests, and CI workflow (#5)
This pull request introduces a comprehensive testing setup for the project, including configuration for Jest, new test scripts, and initial test cases for the `WelcomeScreen` component. Testing configuration and setup: * [`.github/workflows/test.yml`](diffhunk://#diff-faff1af3d8ff408964a57b2e475f69a6b7c7b71c9978cccc8f471798caac2c88R1-R37): Added a GitHub Actions workflow to run tests and linting on every push and pull request to the `main` branch. * [`jest.config.cjs`](diffhunk://#diff-17d090b5ad85e342cd83ee805ef525ea9307d4be9588ac4a7db60042ef6c6d14R1-R24): Created a Jest configuration file to set up the testing environment, module name mappings, and coverage collection. * [`tsconfig.test.json`](diffhunk://#diff-e4fb77850197aa340b1f8f426bb0d445c854e20f0a3dd184050ed555fae92713R1-R10): Added a TypeScript configuration file for tests, specifying compiler options and including test files. * [`src/setupTests.ts`](diffhunk://#diff-84f6be72ed45f1bf4cdbd441d0bd824e38270a7599bd4eac2981b05eb7ad9d29R1): Imported `@testing-library/jest-dom` to extend Jest matchers for DOM node assertions. Test scripts and dependencies: * [`package.json`](diffhunk://#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L10-R13): Added test scripts (`test`, `test:watch`, `test:coverage`) and necessary testing dependencies (`jest`, `ts-jest`, `@testing-library/react`, etc.). [[1]](diffhunk://#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L10-R13) [[2]](diffhunk://#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R31-R34) [[3]](diffhunk://#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R43-R48) Initial test cases: * [`src/components/__tests__/WelcomeScreen.test.tsx`](diffhunk://#diff-48e4b4f1541e0008acd4d6582d5aa07acf9ccc87051f5fb80fdeee891921c535R1-R28): Added test cases for the `WelcomeScreen` component, including rendering tests, event handling tests, and style checks.Introduced Jest-based testing setup with `jest.config.cjs` and required dependencies. Added example test for `WelcomeScreen` component in `src/components/__tests__`. Configured GitHub Actions workflow for running tests, coverage, and linting on push and pull requests to the main branch.
1 parent aed6409 commit c652b7e

File tree

12 files changed

+5825
-1274
lines changed

12 files changed

+5825
-1274
lines changed

.github/workflows/CI.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
test:
11+
name: test
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '20.x'
20+
cache: 'npm'
21+
22+
- name: Install dependencies
23+
run: npm ci
24+
25+
- name: Run tests with coverage
26+
run: npm run test:coverage
27+
28+
# Optional: Add step to save coverage report as an artifact
29+
- name: Upload coverage report
30+
uses: actions/upload-artifact@v4
31+
with:
32+
name: coverage
33+
path: coverage/
34+
retention-days: 7
35+
36+
lint:
37+
name: lint
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@v4
41+
42+
- name: Setup Node.js
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: '20.x'
46+
cache: 'npm'
47+
48+
- name: Install dependencies
49+
run: npm ci
50+
51+
- name: Run linting
52+
run: npm run lint

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ dist-ssr
2323
*.sln
2424
*.sw?
2525

26-
.env
26+
.env
27+
coverage

eslint.config.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,30 @@ import reactRefresh from 'eslint-plugin-react-refresh'
55
import tseslint from 'typescript-eslint'
66

77
export default tseslint.config(
8-
{ ignores: ['dist'] },
9-
{
10-
extends: [js.configs.recommended, ...tseslint.configs.recommended],
11-
files: ['**/*.{ts,tsx}'],
12-
languageOptions: {
13-
ecmaVersion: 2020,
14-
globals: globals.browser,
8+
{ ignores: ['dist', 'coverage/**'] }, // Added coverage directory to ignores
9+
{
10+
extends: [js.configs.recommended, ...tseslint.configs.recommended],
11+
files: ['**/*.{ts,tsx}'],
12+
languageOptions: {
13+
ecmaVersion: 2020,
14+
globals: {
15+
...globals.browser,
16+
React: 'readonly', // Add React global
17+
},
18+
},
19+
plugins: {
20+
'react-hooks': reactHooks,
21+
'react-refresh': reactRefresh,
22+
},
23+
rules: {
24+
...reactHooks.configs.recommended.rules,
25+
'react-refresh/only-export-components': [
26+
'warn',
27+
{ allowConstantExport: true },
28+
],
29+
'no-useless-catch': 'error', // This is for the API file errors
30+
// Optional: If you want to disable the hooks exhaustive-deps warning
31+
// 'react-hooks/exhaustive-deps': 'warn' // or 'off' if you want to disable it
32+
},
1533
},
16-
plugins: {
17-
'react-hooks': reactHooks,
18-
'react-refresh': reactRefresh,
19-
},
20-
rules: {
21-
...reactHooks.configs.recommended.rules,
22-
'react-refresh/only-export-components': [
23-
'warn',
24-
{ allowConstantExport: true },
25-
],
26-
},
27-
},
28-
)
34+
)

jest.config.cjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** @type {import('jest').Config} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'jsdom',
5+
moduleNameMapper: {
6+
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
7+
'^@/(.*)$': '<rootDir>/src/$1'
8+
},
9+
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
10+
transform: {
11+
'^.+\\.tsx?$': ['ts-jest', {
12+
tsconfig: 'tsconfig.test.json'
13+
}]
14+
},
15+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
16+
rootDir: '.',
17+
collectCoverage: true,
18+
collectCoverageFrom: [
19+
'src/**/*.{ts,tsx}',
20+
'!src/**/*.d.ts',
21+
'!src/main.tsx',
22+
'!src/vite-env.d.ts'
23+
],
24+
};

0 commit comments

Comments
 (0)