Skip to content

Commit 3d10611

Browse files
authored
feat: Setup unit tests environment and improve linter (#29)
* feat: Add units tests architecture * chore(deps): Add jest dependencies * chore(linter): Add jest into eslint * chore(linter): Add eslint-plugin-no-comments * chore(linter): Add no-comments/disallowComments * chore(linter): Fix jest * chore(linter): Fix eslintPluginNoComments * chore(linter): Set no-comments on warn * chore(linter): Remove eslint-plugin-no-comments * chore(linter): Add @typescript-eslint/consistent-type-imports * fix(linter): Fix import types * chore(tests): Add jest config * chore(tests): Add test command * chore(deps): Add ts-node dependency * chore(tests): Fix jest config * revert(deps): Remove ts-node dependency * chore(ide): Identify tests folder * chore(ide): Exclude lib folder * chore(tests): Add ts aliases in jest config * test: Create tests for GetAllUsersUseCase * test: Create tests for UsersList * chore(tests): Create a setup file for UI * chore(tests): Create tsconfig.jest.json * test: Fix users-list.test.tsx
1 parent 6b25239 commit 3d10611

File tree

22 files changed

+7910
-2622
lines changed

22 files changed

+7910
-2622
lines changed

.idea/locklite.iml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ lint: node_modules
2828
format: node_modules
2929
npm run format
3030

31+
tests: node_modules
32+
npm run test
33+
3134
migrate: up
3235
npx prisma migrate dev --name init
3336

34-
.PHONY: up down dev build lint format migrate
37+
.PHONY: up down dev build lint format tests migrate
3538

3639
# Aliases
3740
run: up dev

eslint.config.mjs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// eslint.config.mjs
21
import { dirname } from 'path';
32
import { fileURLToPath } from 'url';
43
import { FlatCompat } from '@eslint/eslintrc';
54
import js from '@eslint/js';
65
import tseslint from 'typescript-eslint';
76
import eslintPluginPrettier from 'eslint-plugin-prettier';
87
import importPlugin from 'eslint-plugin-import';
8+
import eslintPluginJest from 'eslint-plugin-jest';
99

1010
const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = dirname(__filename);
@@ -19,6 +19,7 @@ export default tseslint.config(
1919
js.configs.recommended,
2020
...tseslint.configs.recommended,
2121
...compat.extends('next/core-web-vitals', 'next/typescript'),
22+
...compat.extends('plugin:jest/recommended'),
2223

2324
{
2425
ignores: ['node_modules', 'dist', 'jest.config.cjs'],
@@ -34,6 +35,7 @@ export default tseslint.config(
3435
plugins: {
3536
prettier: eslintPluginPrettier,
3637
import: importPlugin,
38+
jest: eslintPluginJest,
3739
},
3840
rules: {
3941
// Formatting
@@ -50,6 +52,7 @@ export default tseslint.config(
5052
'prefer-const': ['error', { destructuring: 'all' }],
5153
'require-object-destructuring': 'off',
5254
'import/no-unresolved': 'error',
55+
'no-inline-comments': 'warn',
5356

5457
// TypeScript strictness
5558
'@typescript-eslint/explicit-member-accessibility': [
@@ -83,6 +86,10 @@ export default tseslint.config(
8386
'@typescript-eslint/class-literal-property-style': ['warn', 'fields'],
8487
'@typescript-eslint/no-empty-function': ['warn'],
8588
'@typescript-eslint/adjacent-overload-signatures': 'warn',
89+
"@typescript-eslint/consistent-type-imports": ["error", {
90+
"prefer": "type-imports",
91+
"disallowTypeAnnotations": false
92+
}],
8693

8794
// Naming conventions
8895
'@typescript-eslint/naming-convention': [
@@ -125,6 +132,17 @@ export default tseslint.config(
125132
},
126133
},
127134
},
135+
136+
{
137+
files: ['**/*.test.ts', '**/*.spec.ts', '**/*.test.tsx', '**/*.spec.tsx'],
138+
plugins: { jest: eslintPluginJest },
139+
languageOptions: {
140+
env: { 'jest/globals': true },
141+
},
142+
settings: {
143+
jest: { version: 29 },
144+
},
145+
},
128146
],
129147
{
130148
files: ['**/*.ts', '**/*.tsx'],

jest.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Config } from 'jest';
2+
3+
const config: Config = {
4+
preset: 'ts-jest',
5+
testEnvironment: 'jsdom',
6+
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
7+
globals: {
8+
'ts-jest': {
9+
tsconfig: 'tsconfig.jest.json',
10+
},
11+
},
12+
transform: {
13+
'^.+\\.(ts|tsx)$': ['ts-jest', {}],
14+
},
15+
roots: ['<rootDir>'],
16+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
17+
moduleNameMapper: {
18+
'^@/(.*)$': '<rootDir>/$1',
19+
'^@api/(.*)$': '<rootDir>/src/modules/api/$1',
20+
'^@ui/(.*)$': '<rootDir>/src/modules/ui/$1',
21+
'^@shared/(.*)$': '<rootDir>/src/modules/shared/$1',
22+
'^@prisma/(.*)$': '<rootDir>/prisma/$1',
23+
'^@lib/(.*)$': '<rootDir>/lib/$1',
24+
},
25+
};
26+
27+
export default config;

jest.setup.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)