Skip to content

Commit a0104aa

Browse files
Copilotdkhalife
andauthored
Configure Jest for unit testing with sample test (#205)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dkhalife <1736645+dkhalife@users.noreply.github.com> Co-authored-by: Dany Khalife <dev@dkhalife.com>
1 parent 33d378b commit a0104aa

File tree

8 files changed

+2572
-23
lines changed

8 files changed

+2572
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ node_modules
66

77
.yarn/
88
.yarnrc.yml
9+
10+
# Test coverage
11+
coverage

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import pluginReact from 'eslint-plugin-react'
66
/** @type {import('eslint').Linter.Config[]} */
77
export default [
88
{ files: ['**/*.{ts,tsx}'] },
9-
{ ignores: ['node_modules', 'dist'] },
9+
{ ignores: ['node_modules', 'dist', 'coverage'] },
1010
{ languageOptions: { globals: globals.browser } },
1111
{ settings: { react: { version: 'detect' } } },
1212
pluginJs.configs.recommended,

jest.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default {
2+
preset: 'ts-jest',
3+
testEnvironment: 'jsdom',
4+
roots: ['<rootDir>/src'],
5+
testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'],
6+
moduleNameMapper: {
7+
'^@/(.*)$': '<rootDir>/src/$1',
8+
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
9+
},
10+
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
11+
collectCoverageFrom: [
12+
'src/**/*.{ts,tsx}',
13+
'!src/**/*.d.ts',
14+
'!src/main.tsx',
15+
'!src/vite.d.ts',
16+
],
17+
coverageDirectory: 'coverage',
18+
coverageReporters: ['text', 'lcov', 'html'],
19+
transform: {
20+
'^.+\\.tsx?$': [
21+
'ts-jest',
22+
{
23+
tsconfig: {
24+
jsx: 'react-jsx',
25+
esModuleInterop: true,
26+
allowSyntheticDefaultImports: true,
27+
},
28+
},
29+
],
30+
},
31+
}

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"start": "vite --host",
88
"build": "vite build --mode prod",
99
"lint": "eslint .",
10-
"preview": "vite preview"
10+
"preview": "vite preview",
11+
"test": "jest",
12+
"test:watch": "jest --watch",
13+
"test:coverage": "jest --coverage"
1114
},
1215
"dependencies": {
1316
"@emotion/react": "^11.14.0",
@@ -25,17 +28,26 @@
2528
},
2629
"devDependencies": {
2730
"@eslint/js": "^9.18.0",
31+
"@testing-library/dom": "^10.4.1",
32+
"@testing-library/jest-dom": "^6.9.1",
33+
"@testing-library/react": "^16.3.0",
34+
"@types/jest": "^30.0.0",
35+
"@types/mocha": "^10.0.10",
2836
"@types/react": "^18",
2937
"@types/react-dom": "^18",
3038
"@vitejs/plugin-react-swc": "^3.7.2",
3139
"autoprefixer": "^10.4.20",
3240
"eslint": "^9.18.0",
3341
"eslint-plugin-react": "^7.37.4",
3442
"globals": "^15.14.0",
43+
"identity-obj-proxy": "^3.0.0",
44+
"jest": "^30.2.0",
45+
"jest-environment-jsdom": "^30.2.0",
3546
"postcss": "^8.4.49",
3647
"prettier": "^3.4.2",
3748
"prettier-plugin-organize-imports": "^4.1.0",
3849
"prettier-plugin-react": "^0.0.1",
50+
"ts-jest": "^29.4.4",
3951
"typescript": "^5.7.3",
4052
"typescript-eslint": "^8.19.1",
4153
"vite": "^6.2.7"

src/Loading.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class Loading extends React.Component<LoadingProps, LoadingState> {
2020
}
2121

2222
componentDidMount() {
23-
this.timeout = setTimeout(() => {
23+
this.timeout = window.setTimeout(() => {
2424
this.setState({
2525
subMessage:
2626
'This is taking longer than usual. There might be an issue.',

src/sample.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe('Sample Test Suite', () => {
2+
it('should pass', () => {
3+
expect(true).toBe(true)
4+
})
5+
6+
it('should perform basic arithmetic', () => {
7+
expect(1 + 1).toBe(2)
8+
})
9+
})

src/setupTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom'

0 commit comments

Comments
 (0)