Skip to content

Commit 0ef4390

Browse files
authored
test(setup): add jest, configure for next.js + typescript (#30)
2 parents 7273f47 + ac0983a commit 0ef4390

17 files changed

+14747
-5108
lines changed

.eslintrc.json

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
{
2-
"extends": ["next", "prettier"]
2+
"extends": [
3+
"next/core-web-vitals",
4+
"plugin:testing-library/react",
5+
"plugin:react-hooks/recommended",
6+
"prettier"
7+
],
8+
"plugins": ["react-hooks"],
9+
"overrides": [
10+
// Only use Testing Library lint rules in test files
11+
{
12+
"files": [
13+
"**/__tests__/**/*.[jt]s?(x)",
14+
"**/?(*.)+(spec|test).[jt]s?(x)"
15+
],
16+
"extends": ["plugin:testing-library/react"]
17+
}
18+
],
19+
"rules": {
20+
"@next/next/no-before-interactive-script-outside-document": "off",
21+
"no-unused-vars": [1, { "args": "after-used", "argsIgnorePattern": "^_" }]
22+
}
323
}

.github/workflows/unit-tests.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: 🧪 Unit Tests (Jest)
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
build:
12+
name: 🧪 Unit Tests (Jest)
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: 📦 Install modules
17+
run: npm install
18+
- name: ⚙️ Run tests
19+
run: npm run test --coverage

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ npm run dev
1414

1515
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
1616

17+
## Tests
18+
19+
### Unit Tests
20+
21+
Run all [Jest](https://jestjs.io/) and [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) unit tests:
22+
23+
```bash
24+
npm run test
25+
```
26+
27+
Launches the test runner in the interactive watch mode.
28+
29+
Tests are colocated and live as closely to corresponding code as possible.
30+
1731
## Deploy
1832

1933
The app is based on [Next.JS](https://nextjs.org/) and is automatically built & deployed to GitHub Pages when pushing to the `main` branch.

jest.config.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const nextJest = require("next/jest");
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 config = {
11+
// Automatically clear mock calls and instances between every test
12+
clearMocks: true,
13+
14+
// Indicates whether the coverage information should be collected while executing the test
15+
collectCoverage: true,
16+
17+
// The directory where Jest should output its coverage files
18+
coverageDirectory: "coverage",
19+
coverageProvider: "v8",
20+
21+
moduleNameMapper: {
22+
// Handle module aliases
23+
"^@/components/(.*)$": "<rootDir>/components/$1",
24+
},
25+
26+
// Add more setup options before each test is run
27+
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
28+
29+
testEnvironment: "jest-environment-jsdom",
30+
31+
testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/.next/"],
32+
33+
transformIgnorePatterns: [
34+
"/node_modules/",
35+
"^.+\\.module\\.(css|sass|scss)$",
36+
],
37+
verbose: true,
38+
};
39+
40+
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
41+
module.exports = createJestConfig(config);

jest.setup.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Learn more: https://github.com/testing-library/jest-dom
2+
import "@testing-library/jest-dom";
3+
import { TextEncoder, TextDecoder } from "util";
4+
5+
Object.assign(global, { TextDecoder, TextEncoder });

0 commit comments

Comments
 (0)