Skip to content

Comments

Optimize test files#334

Open
dgiannico19 wants to merge 3 commits intomasterfrom
optimize-test-files
Open

Optimize test files#334
dgiannico19 wants to merge 3 commits intomasterfrom
optimize-test-files

Conversation

@dgiannico19
Copy link
Contributor

@dgiannico19 dgiannico19 commented Feb 20, 2026

Link al ticket

Descripción del requerimiento

  • Mejorar la legibilidad de los errores de validación que expone el validador
  • Refactorizar los tests del validador: eliminar duplicación, mejorar los mensajes de fallo cuando el schema compilado no coincide con el esperado

Descripción de la solución

Cambios en lib/validator.js

  • Antes: error.errors contenía los objetos crudos de AJV (keyword, dataPath, params, parentSchema, etc), esto hacia imnposible detectar el error com para poder resolverlo rapidamente en ocasiones
  • Ahora: Se añade formateadorformatAjvErrors() y error.errors es un array de strings con formato legible "[n] path message (keyword)".Se comprueba ajv.errors.length antes de lanzar el error

Validar respuesta que recibe el back cuando el schema no ocincide con el esperado

Cambios en index.js

  • Se añade mapErrorToLogLine para que se muestre error.errors de forma legible
  • Soporta tanto el nuevo formato (strings) como objetos, por compatibilidad. Cada error se muestra en una línea

Refactor de tests/validator-test.js

  1. Antes: Un único describe "Test validation functions" con tests repetitivos y un it gigante "should schema builded is a expected" que cargaba +25 schemas a mano y usaba sinon.assert.match
  2. Ahora:
  • Constantes: SCHEMAS_BASE_PATH, DEFAULT_DATA_PATH, etc
  • Loaders: readSchema, loadJsonSchema, loadYamlSchema, loadInputSchema, loadExpectedSchema, getDataPathForSchema
  • Helpers de spies: createValidateCompileSpies, assertSpiesValidateOnly, assertSpiesValidateAndCompile
  • Motor de diffs: findDiffs, formatDiffLine, formatSchemaDiffReport, formatSchemaExecutionErrorReport, runCompilationAndCollectDiffs recorre SCHEMA_COMPILATION_SCHEMAS con try/catch por caso y acumula hasta MAX_SCHEMA_REPORTS (20) con archivo, mensaje y errores AJV
  • Tests reorganizados en describe por intención, root/rootComponent, validate/compile spies, compilación vs expected, errores de validación, default schema
  • Nuevo test que verifica que error.errors es array de strings sin parentSchema ni "properties"
  • Los tests de spies que usaban browse.json ahora usan un schema mínimo válido { root: 'Terminal', url: '...' } para no depender del schema

Funciones

  • findDiffs: Antes sinon.assert.match daba mensajes poco accionables, ahora el reporte muestra path, expected vs received y se trunca a MAX_DIFFS_PER_SCHEMA
  • runCompilationAndCollectDiffs: Usa try/catch por schemaCase para no cortar en el primer error, acumula reportes (hasta 20) con archivo, dataPath, mensaje y errores de validación
  • formatSchemaExecutionErrorReport: Incluye schemaCase.schema.path y schemaCase.expected para identificar rápido cual falló

Comportamiento anterior vs actual

Aspecto Antes Ahora
error.errors en errores del validador Array de objetos AJV (keyword, dataPath, params, parentSchema, etc.) Array de strings "[n] path message (keyword)"
CLI al fallar validación Dependía del formato interno de AJV Una línea por error, legible, soportando strings u objetos
Tests de compilación Un solo it con ~25 schemas y sinon.assert.match Suite data-driven con runCompilationAndCollectDiffs, reportes de diff y de errores de ejecución
Tests de spies Dependían de schema browse.json Schema mínimo { root: 'Terminal', url: '...' }

Mapeo de cobertura de tests (antes → ahora)

Test anterior Test actual
Should error if pass schema with root not defined throws when schema has neither root nor rootComponent
Should error if has a error validation throws when schema has validation errors
Should validate only calls validate only when compile=false
Should compile and validate calls validate and compile when compile=true
Should compile if schema is valid (no compila si falla validación) does not call compile when validation fails
should schema builded is a expected (it gigante) compiled output matches expected for all schemas (vía runCompilationAndCollectDiffs(SCHEMA_COMPILATION_SCHEMAS))
should error with default schema (2 asserts) throws when schema has invalid root type + throws when schema has no root and no url
should pass validation and build data with default schema passes validation and compiles with valid default schema

No se perdió cobertura; los escenarios se mantienen con nombres y estructura más claros

Cómo se puede probar?

Caso Resultado esperado Resultado obtenido Observaciones
Ejecutar npm run test Todos los tests pasan Pendiente Suite refactorizada
Validar un schema inválido Error con error.errors array de strings legibles Pendiente Se acumulan errores siendo claros en qué archivo suceden

Datos extra a tener en cuenta

  • Estaria bueno saber si el error al back le sirve mejorado o si necesitan algun dato mas como para detectar claramente que sucede y donde

Changelog

### Changed
- Validator test file optimization
- Errors manage

Summary by CodeRabbit

Release Notes

  • Improvements
    • Error messages are now formatted in a more structured and readable manner, with better organization for easier comprehension

@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2026

📝 Walkthrough

Walkthrough

Error handling and validation are enhanced through new utility methods for normalizing error output. A helper function formats error items in index.js, a static method standardizes AJV error formatting in Validator, and tests are refactored from hardcoded assertions to a data-driven framework with reusable schema loading and diff comparison utilities.

Changes

Cohort / File(s) Summary
Error Logging Enhancement
index.js
Introduces mapErrorToLogLine() helper to transform error items into indexed, human-readable log lines. Maps error array through this function before logging instead of directly logging raw error objects.
AJV Error Formatting Utility
lib/validator.js
Adds static method Validator.formatAjvErrors(errors) to normalize AJV validation errors into formatted strings with index, path, message, and keyword. Updates error checking and throwing logic to use formatted errors.
Test Suite Refactoring
tests/validator-test.js
Converts validator tests from inline hardcoded schema assertions to data-driven framework. Introduces centralized schema loading utilities, diff computation, error formatting helpers, and consolidated multi-case validation test structure with enhanced error reporting.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

enhancement

Poem

🐰 Errors now hop with purpose bright,
Formatted cleanly, trimmed just right!
Tests unified in a data-driven dance,
Each validation given its rightful chance—
From AJV depths to logs that gleam,
A harmonious, error-handling dream!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is vague and overly generic, using non-descriptive terms that don't convey the main changes made in this PR. Use a more specific title such as 'Format validation errors and refactor validator tests' to clearly communicate the primary objectives.
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The PR description is comprehensive, well-structured, and follows the template with detailed explanations of requirements, solutions, test coverage mapping, and changelog.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch optimize-test-files

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cursor
Copy link

cursor bot commented Feb 20, 2026

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on March 12.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@coderabbitai coderabbitai bot added the enhancement New feature or request label Feb 20, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
index.js (1)

101-105: Prefer .forEach() over .map() for side-effect-only iteration.

Line 104 uses .map() to call logger.error, but the returned array is discarded. .forEach() communicates intent more clearly and avoids an unnecessary allocation.

♻️ Suggested diff
 		if(error.errors) {
 			error.errors
 				.map(mapErrorToLogLine)
-				.map(errorLine => logger.error(errorLine));
+				.forEach(errorLine => logger.error(errorLine));
 		}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@index.js` around lines 101 - 105, The error handling code uses .map() twice
for side effects and discards the results; replace the second .map() that calls
logger.error with .forEach() (and you can also replace the first
.map()->.forEach() if you prefer a single-step approach) so that error.errors is
transformed via mapErrorToLogLine and each resulting line is logged via
logger.error without allocating an unused array; update the block around
error.errors, mapErrorToLogLine and logger.error accordingly to reflect intent.
tests/validator-test.js (2)

158-164: Inconsistent language: Spanish text in English codebase.

Lines 161 and 176 contain Spanish truncation notes ("diffs adicionales truncados", "errores de validacion truncados"), while the rest of the codebase and comments are in English.

♻️ Suggested diff
 	const truncationNote = diffs.length >= MAX_DIFFS_PER_SCHEMA
-		? '\n... (diffs adicionales truncados)'
+		? '\n... (additional diffs truncated)'
 		: '';

And similarly in formatSchemaExecutionErrorReport:

 	const validationTruncationNote = Array.isArray(error && error.errors) && error.errors.length > MAX_DIFFS_PER_SCHEMA
-		? '\n... (errores de validacion truncados)'
+		? '\n... (validation errors truncated)'
 		: '';
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/validator-test.js` around lines 158 - 164, The truncation note strings
in formatSchemaDiffReport and formatSchemaExecutionErrorReport are in Spanish;
change them to English for consistency (replace '\n... (diffs adicionales
truncados)' with something like '\n... (additional diffs truncated)' and replace
the validation error truncation message in formatSchemaExecutionErrorReport
('\n... (errores de validacion truncados)') with an English equivalent such as
'\n... (validation errors truncated)') so all messages in these functions are in
English; update only the literal strings in formatSchemaDiffReport and
formatSchemaExecutionErrorReport.

312-319: All schema cases in a single test — tradeoff worth noting.

Running all 25 schemas in one test via runCompilationAndCollectDiffs gives a consolidated report, but a systemic failure produces a single massive error rather than 25 individual test failures. This makes it harder to track regressions in CI (no per-schema pass/fail granularity). The current approach is acceptable for catching fixture mismatches in aggregate, but consider whether parameterized individual tests (e.g., with a loop generating it(...) blocks) would give better CI signal long-term.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/validator-test.js` around lines 312 - 319, Current test bundles all 25
schemas into one it() using
runCompilationAndCollectDiffs(SCHEMA_COMPILATION_SCHEMAS), which yields a single
large failure on systemic breakage; change the test to generate a separate
it(...) per schema (iterate SCHEMA_COMPILATION_SCHEMAS with forEach or a for
loop inside the describe('schema compilation vs expected') block), call
runCompilationAndCollectDiffs or an adapted helper per single schema entry, and
assert/fail per test so each schema (identified by its fixture name) reports
pass/fail independently; keep the existing failure formatting but include the
schema identifier in each test's assertion message to aid CI diagnostics.
lib/validator.js (1)

13-21: formatAjvErrors(null) would bypass the default and throw.

The default parameter errors = [] only applies when errors is undefined, not null. Since AJV v6 sets ajv.errors to null on success, a future caller passing null directly would cause null.map() to throw. Currently safe because line 65 guards against it, but consider a defensive normalization.

🛡️ Defensive normalization
 static formatAjvErrors(errors = []) {
-    return errors.map((currentError, index) => {
+    return (errors || []).map((currentError, index) => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/validator.js` around lines 13 - 21, The formatAjvErrors function can
receive null (AJV uses null for no errors) which bypasses the default parameter
and causes a crash when calling .map; in formatAjvErrors normalize the incoming
errors (inside the static method) to an array first—e.g., use
Array.isArray(errors) ? errors : [] or coerce with errors || []—then iterate
over that normalized array so formatAjvErrors and callers are safe when passed
null or undefined; update the implementation of formatAjvErrors accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/validator-test.js`:
- Around line 171-184: The template builds validationLines as an array which
gets coerced to a comma-separated string when interpolated; inside
formatSchemaExecutionErrorReport, change the handling of validationLines so it
becomes a single string (e.g., join the mapped lines with '' or '\n') instead of
leaving it as an array — update the code that defines validationLines (and any
use in the final template) in formatSchemaExecutionErrorReport to call
.join(...) after .map(...) so the validation error lines render without unwanted
commas while preserving MAX_DIFFS_PER_SCHEMA truncation logic and the
validationTruncationNote behavior.

---

Nitpick comments:
In `@index.js`:
- Around line 101-105: The error handling code uses .map() twice for side
effects and discards the results; replace the second .map() that calls
logger.error with .forEach() (and you can also replace the first
.map()->.forEach() if you prefer a single-step approach) so that error.errors is
transformed via mapErrorToLogLine and each resulting line is logged via
logger.error without allocating an unused array; update the block around
error.errors, mapErrorToLogLine and logger.error accordingly to reflect intent.

In `@lib/validator.js`:
- Around line 13-21: The formatAjvErrors function can receive null (AJV uses
null for no errors) which bypasses the default parameter and causes a crash when
calling .map; in formatAjvErrors normalize the incoming errors (inside the
static method) to an array first—e.g., use Array.isArray(errors) ? errors : []
or coerce with errors || []—then iterate over that normalized array so
formatAjvErrors and callers are safe when passed null or undefined; update the
implementation of formatAjvErrors accordingly.

In `@tests/validator-test.js`:
- Around line 158-164: The truncation note strings in formatSchemaDiffReport and
formatSchemaExecutionErrorReport are in Spanish; change them to English for
consistency (replace '\n... (diffs adicionales truncados)' with something like
'\n... (additional diffs truncated)' and replace the validation error truncation
message in formatSchemaExecutionErrorReport ('\n... (errores de validacion
truncados)') with an English equivalent such as '\n... (validation errors
truncated)') so all messages in these functions are in English; update only the
literal strings in formatSchemaDiffReport and formatSchemaExecutionErrorReport.
- Around line 312-319: Current test bundles all 25 schemas into one it() using
runCompilationAndCollectDiffs(SCHEMA_COMPILATION_SCHEMAS), which yields a single
large failure on systemic breakage; change the test to generate a separate
it(...) per schema (iterate SCHEMA_COMPILATION_SCHEMAS with forEach or a for
loop inside the describe('schema compilation vs expected') block), call
runCompilationAndCollectDiffs or an adapted helper per single schema entry, and
assert/fail per test so each schema (identified by its fixture name) reports
pass/fail independently; keep the existing failure formatting but include the
schema identifier in each test's assertion message to aid CI diagnostics.

Comment on lines +171 to +184
const formatSchemaExecutionErrorReport = (schemaCase, error) => {
const validationLines = Array.isArray(error && error.errors)
? error.errors.slice(0, MAX_DIFFS_PER_SCHEMA).map(line => `${line}\n`)
: '';
const validationTruncationNote = Array.isArray(error && error.errors) && error.errors.length > MAX_DIFFS_PER_SCHEMA
? '\n... (errores de validacion truncados)'
: '';

return (
`[SCHEMA CASE] ${schemaCase.schema.path} -> ${schemaCase.expected}\n` +
`Error message: ${(error && error.message) || 'Unknown execution error'}` +
(validationLines ? `\nvalidation errors:\n${validationLines}${validationTruncationNote}` : '')
);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Bug: Array-to-string coercion inserts unwanted commas between validation error lines.

On line 173, error.errors.slice(...).map(line => ...) produces an array. When this array is interpolated into the template literal on line 182, JavaScript calls .toString() (i.e., .join(',')) — inserting commas between each error line.

🐛 Proposed fix
 const validationLines = Array.isArray(error && error.errors)
-    ? error.errors.slice(0, MAX_DIFFS_PER_SCHEMA).map(line => `${line}\n`)
+    ? error.errors.slice(0, MAX_DIFFS_PER_SCHEMA).map(line => `${line}\n`).join('')
     : '';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const formatSchemaExecutionErrorReport = (schemaCase, error) => {
const validationLines = Array.isArray(error && error.errors)
? error.errors.slice(0, MAX_DIFFS_PER_SCHEMA).map(line => `${line}\n`)
: '';
const validationTruncationNote = Array.isArray(error && error.errors) && error.errors.length > MAX_DIFFS_PER_SCHEMA
? '\n... (errores de validacion truncados)'
: '';
return (
`[SCHEMA CASE] ${schemaCase.schema.path} -> ${schemaCase.expected}\n` +
`Error message: ${(error && error.message) || 'Unknown execution error'}` +
(validationLines ? `\nvalidation errors:\n${validationLines}${validationTruncationNote}` : '')
);
};
const formatSchemaExecutionErrorReport = (schemaCase, error) => {
const validationLines = Array.isArray(error && error.errors)
? error.errors.slice(0, MAX_DIFFS_PER_SCHEMA).map(line => `${line}\n`).join('')
: '';
const validationTruncationNote = Array.isArray(error && error.errors) && error.errors.length > MAX_DIFFS_PER_SCHEMA
? '\n... (errores de validacion truncados)'
: '';
return (
`[SCHEMA CASE] ${schemaCase.schema.path} -> ${schemaCase.expected}\n` +
`Error message: ${(error && error.message) || 'Unknown execution error'}` +
(validationLines ? `\nvalidation errors:\n${validationLines}${validationTruncationNote}` : '')
);
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/validator-test.js` around lines 171 - 184, The template builds
validationLines as an array which gets coerced to a comma-separated string when
interpolated; inside formatSchemaExecutionErrorReport, change the handling of
validationLines so it becomes a single string (e.g., join the mapped lines with
'' or '\n') instead of leaving it as an array — update the code that defines
validationLines (and any use in the final template) in
formatSchemaExecutionErrorReport to call .join(...) after .map(...) so the
validation error lines render without unwanted commas while preserving
MAX_DIFFS_PER_SCHEMA truncation logic and the validationTruncationNote behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant