2 Commits

Author SHA1 Message Date
Valery Petrov 052d843dbd issues/27: add formatApiError util and unit tests 2026-06-04 13:19:02 +03:00
Valery Petrov cfd8a4d403 issues/27: CI Jest coverage on test and stage tags 2026-06-04 12:51:57 +03:00
4 changed files with 84 additions and 20 deletions
+37 -20
View File
@@ -1,6 +1,6 @@
name: adminpanel-ci-cd
# CI/CD: только push git-тега (ручное тегирование на ветке prod|test|stage).
# Pre-deploy: Jest + coverage on test|stage tags. Prod — без автотестов.
on:
push:
@@ -13,18 +13,6 @@ env:
IMAGE_DEPLOY: git.sova.local/sova/adminpanel
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '24'
- name: Install dependencies
run: |
if [ -f package-lock.json ]; then npm ci; else npm install; fi
- run: npm run build
parse-tag:
runs-on: ubuntu-latest
outputs:
@@ -40,8 +28,42 @@ jobs:
echo "env=$(echo "$TAG" | sed -E 's/adminpanel-v([0-9.]+)-([a-z]+)/\2/')" >> "$GITHUB_OUTPUT"
echo "version=$(echo "$TAG" | sed -E 's/adminpanel-v([0-9.]+).*/\1/')" >> "$GITHUB_OUTPUT"
test:
needs: [parse-tag]
if: needs.parse-tag.outputs.env != 'prod'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '24'
- name: Install dependencies
run: |
if [ -f package-lock.json ]; then npm ci; else npm install; fi
- name: Lint
run: npm run lint
- name: Unit tests + coverage
run: npm run test:ci
- name: Build
run: npm run build
- name: Upload coverage
if: always()
uses: actions/upload-artifact@v4
with:
name: adminpanel-coverage-${{ needs.parse-tag.outputs.full_tag }}
path: coverage/
retention-days: 14
test-prod-skip:
needs: [parse-tag]
if: needs.parse-tag.outputs.env == 'prod'
runs-on: ubuntu-latest
steps:
- run: echo "Prod tag — automated tests skipped."
build-and-push:
needs: [test, parse-tag]
needs: [parse-tag, test, test-prod-skip]
if: always() && (needs.test.result == 'success' || needs.test.result == 'skipped') && (needs.test-prod-skip.result == 'success' || needs.test-prod-skip.result == 'skipped')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
@@ -95,13 +117,8 @@ jobs:
git add "apps/adminpanel/values-${ENV}.yaml"
git diff --cached --quiet && { echo "No changes"; exit 0; }
git commit -m "chore(adminpanel): bump ${ENV} to ${TAG}"
if git push origin "${ENV}"; then
echo "Push OK on attempt ${attempt}"
exit 0
fi
echo "Push failed, retry ${attempt}/${MAX_RETRIES}..."
if git push origin "${ENV}"; then exit 0; fi
git reset --hard HEAD~1
sleep $((attempt * 2))
done
echo "Failed to push after ${MAX_RETRIES} attempts"
exit 1
+1
View File
@@ -10,6 +10,7 @@
"lint-fix": "eslint . --fix",
"preview": "vite preview",
"test": "jest --runInBand",
"test:ci": "jest --runInBand --coverage --coverageReporters=text-summary --coverageReporters=lcov",
"test-watch": "jest --watch",
"test-clear-cache": "jest --clearCache"
},
+19
View File
@@ -0,0 +1,19 @@
import { formatApiError } from '../formatApiError';
describe('formatApiError', () => {
it('returns string errors as-is', () => {
expect(formatApiError('bad')).toBe('bad');
});
it('reads RTK-style data.message', () => {
expect(formatApiError({ data: { message: 'Validation failed' } })).toBe('Validation failed');
});
it('reads axios-style response.data.error', () => {
expect(formatApiError({ response: { data: { error: 'Unauthorized' } } })).toBe('Unauthorized');
});
it('falls back to error.message', () => {
expect(formatApiError({ message: 'Network' })).toBe('Network');
});
});
+27
View File
@@ -0,0 +1,27 @@
/**
* Normalize API error payload for UI (RTK Query / axios).
* @param {unknown} error
* @returns {string}
*/
export function formatApiError(error) {
if (!error) {
return 'Unknown error';
}
if (typeof error === 'string') {
return error;
}
const data = error?.data ?? error?.response?.data;
if (typeof data === 'string') {
return data;
}
if (data?.message) {
return String(data.message);
}
if (data?.error) {
return String(data.error);
}
if (error?.message) {
return String(error.message);
}
return 'Request failed';
}