diff --git a/packages/langium/src/test/langium-test.ts b/packages/langium/src/test/langium-test.ts index 19ec41cc7..af42694e3 100644 --- a/packages/langium/src/test/langium-test.ts +++ b/packages/langium/src/test/langium-test.ts @@ -21,6 +21,7 @@ import * as assert from 'node:assert'; import { stream } from '../utils/stream.js'; import { Disposable } from '../utils/disposable.js'; import { normalizeEOL } from '../generate/template-string.js'; +import { DocumentValidator } from '../validation/document-validator.js'; export interface ParseHelperOptions extends BuildOptions { /** @@ -550,18 +551,29 @@ export interface ValidationResult extends AsyncDisp document: LangiumDocument; } -export function validationHelper(services: LangiumCoreServices): (input: string, options?: ParseHelperOptions) => Promise> { +export type ValidationHelperOptions = ParseHelperOptions & { failOnParsingErrors?: boolean }; + +export function validationHelper(services: LangiumCoreServices): (input: string, options?: ValidationHelperOptions) => Promise> { const parse = parseHelper(services); return async (input, options) => { const document = await parse(input, { ...(options ?? {}), validation: true }); - return { + const result = { document, diagnostics: document.diagnostics ?? [], dispose: () => clearDocuments(services, [document]) }; + if (options?.failOnParsingErrors) { + expectNoIssues(result, { + severity: DiagnosticSeverity.Error, + data: { + code: DocumentValidator.ParsingError, + }, + }); + } + return result; }; }