# test.govlab/code-parse/core/converters/syntax.converter.test.ts

> 47 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-govlab-code-parse-core-converters-syntax-converter-test-ts
Source text: https://banes-lab.com/source/testing/test.govlab/code-parse/core/converters/syntax.converter.test.ts.txt

Listed in [test.govlab/code-parse/core/converters](https://banes-lab.com/api/source/testing/test.govlab/code-parse/core/converters.md), after [test.govlab/code-parse/core/converters/grammar.converter.test.ts](https://banes-lab.com/source/testing/test.govlab/code-parse/core/converters/grammar.converter.test.ts.md).

## Contained in

- [test.govlab/code-parse/core/converters](https://banes-lab.com/anatomy/testing/folder-testing-test-govlab-code-parse-core-converters.md)

## Source

```typescript
import {
    NO_TREE,
    PARSE_FAILED,
    PARSE_INCOMPLETE,
    noGrammar,
    notPreloaded,
} from "@govlab/code-parse/configuration/strings/syntax.strings.ts";
import { beforeAll, describe, expect, it } from "vitest";
import { commentNodes, ensureLanguages, parseCode, parseCodeSync } from "@govlab/code-parse";
import { PARSE_ATTEMPTS } from "@govlab/code-parse/configuration/constants/syntax.constants.ts";

describe("the syntax converter", () => {
    beforeAll(async () => {
        await ensureLanguages(["go", "javascript", "typescript"]);
    });

    it("parses synchronously after preload and locates comment nodes with byte ranges", () => {
        const source = "package main\n\n// a standalone comment\nfunc main() {}\n";
        const root = parseCodeSync(source, "go");
        const [comment] = root === null ? [] : commentNodes(root);
        expect(comment?.startIndex).toBeTypeOf("number");
        expect(source.slice(comment?.startIndex, comment?.endIndex)).toContain("a standalone comment");
    });

    it("parseCode loads a grammar on demand", async () => {
        const root = await parseCode("const x = 1; // inline\n", "javascript");
        expect(root === null ? [] : commentNodes(root)).toHaveLength(1);
    });

    it("warns and answers null for a language with no grammar", async () => {
        const warnings: string[] = [];
        const root = await parseCode("x", "definitely-not-a-language", {
            logger: {
                warn: (message) => {
                    warnings.push(message);
                },
            },
        });
        expect(root).toBeNull();
        expect(warnings).toStrictEqual([noGrammar("definitely-not-a-language")]);
    });

    it("throws when parsing synchronously without preloading the grammar", () => {
        expect(() => parseCodeSync("SELECT 1", "definitely-not-a-language")).toThrow(
            notPreloaded("definitely-not-a-language"),
        );
    });

    it("retries a bounded number of times and names each diagnostic", () => {
        expect(PARSE_ATTEMPTS).toBeGreaterThan(1);
        expect(new Set([NO_TREE, PARSE_FAILED, PARSE_INCOMPLETE]).size).toBe(3);
    });
});
```
