Skip to content

Commit

Permalink
feat: DATA-000 Setup testing library
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-rmalyavc committed May 27, 2024
1 parent 3f006b3 commit b9f676d
Show file tree
Hide file tree
Showing 8 changed files with 3,935 additions and 383 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = {
parserOptions: {
project: path.join(__dirname, 'tsconfig.json'),
},
plugins: ['@typescript-eslint'],
plugins: ['@typescript-eslint', 'jest'],
extends: ['next/core-web-vitals', 'plugin:@typescript-eslint/recommended'],
rules: {
'@typescript-eslint/consistent-type-imports': [
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
node-version: ${{ matrix.node-version }}

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

- name: Lint
run: SKIP_ENV_VALIDATION=true npm run lint
Expand Down
20 changes: 20 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from 'jest'
import nextJest from 'next/jest.js'

const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})

const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
setupFilesAfterEnv: ['./jest.setup.ts'],
setupFiles: ['./jest.setup.ts']
}

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config);
5 changes: 5 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Response, Request, Headers } from '@whatwg-node/fetch';

global.Response = Response;
global.Request = Request;
global.Headers = Headers;
4,251 changes: 3,873 additions & 378 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"start": "next start",
"type-check": "tsc --pretty --noEmit",
"format": "prettier --write **/*.{js,ts,tsx}",
"postinstall": "node ./postinstall.mjs"
"postinstall": "node ./postinstall.mjs",
"test": "jest",
"test:watch": "jest --watch"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -54,20 +56,29 @@
"zod": "^3.22.3"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@types/eslint": "^8.37.0",
"@types/jest": "^29.5.12",
"@types/node": "18.18.4",
"@types/react": "^18.2.6",
"@types/react-dom": "^18.2.4",
"@typescript-eslint/eslint-plugin": "^5.59.6",
"@typescript-eslint/parser": "^5.59.6",
"@whatwg-node/fetch": "^0.9.17",
"eslint": "^8.40.0",
"eslint-config-next": "^13.4.2",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-jest": "^28.5.0",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"lint-staged": "^13.2.3",
"prettier": "^3.0.1",
"request": "^2.88.2",
"ts-jest": "^29.1.3",
"ts-node": "^10.9.2",
"typescript": "^5.0.4"
},
"eslintIgnore": [
Expand Down
19 changes: 19 additions & 0 deletions src/app/page.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { render } from '@testing-library/react';
import Page from './page';

const mockHomePage = jest.fn(() => <div>Mock Home Page</div>);

jest.mock('./home-page', () => ({
__esModule: true,
default: () => mockHomePage(),
}));

describe('Page', () => {
it('renders Home page', () => {
render(<Page />);

expect(mockHomePage).toHaveBeenCalled();
});
});

6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"**/*.cjs",
"**/*.mjs",
".next/types/**/*.ts",
"postinstall.mjs"
"postinstall.mjs",
"jest.config.js"
],
"exclude": ["node_modules"]
"exclude": ["node_modules"],
"types": ["jest", "node"]
}

0 comments on commit b9f676d

Please sign in to comment.