Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(unit): setup jest, add tests + gh workflow #71

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 🧪 Unit Tests (Jest)

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
name: 🧪 Unit Tests (Jest)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 📦 Install modules
run: npm install
- name: ⚙️ Run tests
run: npm run test --coverage
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ npm run dev

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

## Tests

### Unit Tests

Run all [Jest](https://jestjs.io/) and [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) unit tests:

```bash
npm run test
```

Launches the test runner in the interactive watch mode.

Tests are co-located and live as closely to corresponding code as possible.

## Deploy

The app is based on [Next.JS](https://nextjs.org/) and is automatically built & deployed to GitHub Pages when pushing to the `main` branch.
Expand Down
45 changes: 45 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const nextJest = require("next/jest");

const createJestConfig = nextJest({
dir: "./",
});

/** @type {import('jest').Config} */
const config = {
// automatically clear mock calls and instances between every test
clearMocks: true,

// whether the coverage information should be collected while executing the test
collectCoverage: true,

// directory where Jest should output its coverage files
coverageDirectory: "coverage",
coverageProvider: "v8",

globals: {
"ts-jest": {
tsconfig: "<rootDir>/tsconfig.test.json",
},
},

moduleNameMapper: {
// handle module aliases
"^@/components/(.*)$": "<rootDir>/components/$1",
},

// add more setup options before each test is run
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],

testEnvironment: "jest-environment-jsdom",

testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/.next/"],

transformIgnorePatterns: [
"/node_modules/",
"^.+\\.module\\.(css|sass|scss)$",
],
verbose: true,
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(config);
5 changes: 5 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom";
import { TextEncoder, TextDecoder } from "util";

Object.assign(global, { TextDecoder, TextEncoder });
Loading
Loading