From c1c7eb006bb7a5172ad56da88af9f9048b3d3604 Mon Sep 17 00:00:00 2001 From: Developer Date: Wed, 18 Mar 2026 16:19:03 +0800 Subject: [PATCH] chore: add ESLint, Prettier and Jest configuration --- .eslintignore | 7 ++++ .eslintrc.js | 19 ++++++++++ .prettierrc | 9 +++++ jest.config.js | 6 ++++ server/__tests__/validators.test.js | 55 +++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 .eslintignore create mode 100644 .eslintrc.js create mode 100644 .prettierrc create mode 100644 jest.config.js create mode 100644 server/__tests__/validators.test.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..aaed6a0 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,7 @@ +node_modules/ +frontend/node_modules/ +frontend/.next/ +logs/ +*.log +accountbook.db +.worktrees/ diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..3267031 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,19 @@ +module.exports = { + root: true, + env: { + node: true, + es2021: true, + jest: true, + }, + extends: ['eslint:recommended'], + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + }, + rules: { + 'no-console': 'off', + 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], + 'prefer-const': 'error', + 'no-var': 'error', + }, +}; diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..2d30e11 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,9 @@ +{ + "semi": true, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "es5", + "printWidth": 100, + "bracketSpacing": true, + "arrowParens": "always" +} diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..7490637 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + testEnvironment: 'node', + roots: ['/server'], + testMatch: ['**/__tests__/**/*.test.js'], + verbose: true, +}; diff --git a/server/__tests__/validators.test.js b/server/__tests__/validators.test.js new file mode 100644 index 0000000..a4b3b19 --- /dev/null +++ b/server/__tests__/validators.test.js @@ -0,0 +1,55 @@ +const { + isNonEmptyString, + isValidMonth, + isValidDateTime, + validateRecordPayload, +} = require('../utils/validators'); + +describe('Validators', () => { + describe('isNonEmptyString', () => { + test('returns true for non-empty string', () => { + expect(isNonEmptyString('hello')).toBe(true); + }); + + test('returns false for empty string', () => { + expect(isNonEmptyString('')).toBe(false); + }); + + test('returns false for non-string', () => { + expect(isNonEmptyString(123)).toBe(false); + expect(isNonEmptyString(null)).toBe(false); + }); + }); + + describe('isValidMonth', () => { + test('returns true for valid month format', () => { + expect(isValidMonth('2024-01')).toBe(true); + }); + + test('returns false for invalid month format', () => { + expect(isValidMonth('2024-1')).toBe(false); + }); + }); + + describe('validateRecordPayload', () => { + test('returns null for valid payload', () => { + const payload = { + type: 'expense', + amount: 100, + category: '餐饮', + date: '2024-01-15', + }; + expect(validateRecordPayload(payload)).toBeNull(); + }); + + test('returns error for invalid type', () => { + const payload = { + type: 'invalid', + amount: 100, + category: '餐饮', + date: '2024-01-15', + }; + expect(validateRecordPayload(payload)).toBe('账目类型错误'); + }); + }); +});