diff --git a/packages/parser/tests/info.test.ts b/packages/parser/tests/info.test.ts index 679c46793..df86ae16c 100644 --- a/packages/parser/tests/info.test.ts +++ b/packages/parser/tests/info.test.ts @@ -3,10 +3,11 @@ import type { LangiumParser, ParseResult } from 'langium'; import type { InfoServices } from '../src/language/index.js'; import { Info, createInfoServices } from '../src/language/index.js'; +import { noErrorsOrAlternatives } from './test-util.js'; const services: InfoServices = createInfoServices().Info; const parser: LangiumParser = services.parser.LangiumParser; -export function createInfoTestServices(): { +function createInfoTestServices(): { services: InfoServices; parse: (input: string) => ParseResult; } { @@ -30,11 +31,10 @@ describe('info', () => { info `, ])('should handle empty info', (context: string) => { - const { parserErrors, lexerErrors, value } = parse(context); - expect(parserErrors).toHaveLength(0); - expect(lexerErrors).toHaveLength(0); + const result = parse(context); + noErrorsOrAlternatives(result); - expect(value.$type).toBe(Info); + expect(result.value.$type).toBe(Info); }); it.each([ @@ -49,10 +49,9 @@ describe('info', () => { showInfo `, ])('should handle showInfo', (context: string) => { - const { parserErrors, lexerErrors, value } = parse(context); - expect(parserErrors).toHaveLength(0); - expect(lexerErrors).toHaveLength(0); + const result = parse(context); + noErrorsOrAlternatives(result); - expect(value.$type).toBe(Info); + expect(result.value.$type).toBe(Info); }); }); diff --git a/packages/parser/tests/test-util.ts b/packages/parser/tests/test-util.ts new file mode 100644 index 000000000..0eca737af --- /dev/null +++ b/packages/parser/tests/test-util.ts @@ -0,0 +1,19 @@ +import { ParseResult } from 'langium'; + +const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => { + return; +}); + +/** + * A helper test function that validate that the result doesn't have errors + * or any ambiguous alternatives from chevrotain. + * + * @param result - the result `parse` function. + */ +export function noErrorsOrAlternatives(result: ParseResult) { + expect(result.lexerErrors).toHaveLength(0); + expect(result.parserErrors).toHaveLength(0); + + expect(consoleMock).not.toHaveBeenCalled(); + consoleMock.mockReset(); +}