chore: add ESLint, Prettier and Jest configuration
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
node_modules/
|
||||
frontend/node_modules/
|
||||
frontend/.next/
|
||||
logs/
|
||||
*.log
|
||||
accountbook.db
|
||||
.worktrees/
|
||||
@@ -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',
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5",
|
||||
"printWidth": 100,
|
||||
"bracketSpacing": true,
|
||||
"arrowParens": "always"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
testEnvironment: 'node',
|
||||
roots: ['<rootDir>/server'],
|
||||
testMatch: ['**/__tests__/**/*.test.js'],
|
||||
verbose: true,
|
||||
};
|
||||
@@ -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('账目类型错误');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user