Skip to content

Commit 9bfd4cc

Browse files
committed
setup jest
1 parent 586b09c commit 9bfd4cc

7 files changed

+2280
-32
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"@typescript-eslint/naming-convention": [
2828
"error",
2929
{
30-
"selector": "interface", "format": ["PascalCase"]
30+
"selector": "interface",
31+
"format": ["PascalCase"]
3132
}
3233
],
3334
"react/jsx-pascal-case": 2,

jest.config.mjs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import nextJest from "next/jest.js";
2+
3+
const createJestConfig = nextJest({
4+
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
5+
dir: "./",
6+
});
7+
8+
// Add any custom config to be passed to Jest
9+
/** @type {import('jest').Config} */
10+
const sharedConfig = {
11+
// Add more setup options before each test is run
12+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
13+
coveragePathIgnorePatterns: ["<rootDir>/build/", "<rootDir>/node_modules/"],
14+
moduleNameMapper: {
15+
"^@/(.*)$": "<rootDir>/src/$1",
16+
},
17+
};
18+
19+
const clientTestConfig = {
20+
...sharedConfig,
21+
testEnvironment: "jest-environment-jsdom",
22+
testMatch: ["**/src/{app,components,store}/**/*.test.ts?(x)"],
23+
};
24+
25+
const config = {
26+
// Add more setup options before each test is run
27+
projects: [await createJestConfig(clientTestConfig)()],
28+
};
29+
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
30+
export default config;

jest.setup.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@testing-library/jest-dom/extend-expect";

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"start": "next start",
99
"lint": "next lint",
1010
"prettier": "prettier . --write",
11-
"prepare": "husky install"
11+
"prepare": "husky install",
12+
"test": "jest --watch"
1213
},
1314
"dependencies": {
1415
"@reduxjs/toolkit": "^1.9.5",
@@ -19,6 +20,8 @@
1920
},
2021
"devDependencies": {
2122
"@next/eslint-plugin-next": "^13.4.12",
23+
"@testing-library/jest-dom": "^5.17.0",
24+
"@testing-library/react": "^14.0.0",
2225
"@types/node": "20.4.5",
2326
"@types/react": "18.2.18",
2427
"@types/react-dom": "18.2.7",
@@ -33,6 +36,8 @@
3336
"eslint-plugin-import": "^2.28.0",
3437
"eslint-plugin-react": "^7.33.1",
3538
"husky": "^8.0.0",
39+
"jest": "^29.6.2",
40+
"jest-environment-jsdom": "^29.6.2",
3641
"postcss": "8.4.27",
3742
"prettier": "^3.0.1",
3843
"tailwindcss": "3.3.3",

src/app/counter/counter.test.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { render, screen } from "@testing-library/react";
2+
import CounterPage from "./page";
3+
4+
it("should render the component", () => {
5+
render(<CounterPage />);
6+
7+
const button = screen.getByRole("button", { name: "+" });
8+
9+
expect(button).toBeInTheDocument();
10+
});

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"@/*": ["./src/*"]
2424
}
2525
},
26-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
26+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "jest.config.mjs"],
2727
"exclude": ["node_modules"]
2828
}

0 commit comments

Comments
 (0)