# test.govlab/patterns/core/parsers/definition.parser.test.ts

> 32 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-govlab-patterns-core-parsers-definition-parser-test-ts
Source text: https://banes-lab.com/source/testing/test.govlab/patterns/core/parsers/definition.parser.test.ts.txt

Listed in [test.govlab/patterns/core/parsers](https://banes-lab.com/api/source/testing/test.govlab/patterns/core/parsers.md), after [test.govlab/patterns/core/parsers/code.parser.test.ts](https://banes-lab.com/source/testing/test.govlab/patterns/core/parsers/code.parser.test.ts.md) and before [test.govlab/patterns/core/parsers/dependency.parser.test.ts](https://banes-lab.com/source/testing/test.govlab/patterns/core/parsers/dependency.parser.test.ts.md).

## Contained in

- [test.govlab/patterns/core/parsers](https://banes-lab.com/anatomy/testing/folder-testing-test-govlab-patterns-core-parsers.md)

## Source

```typescript
import { describe, expect, it } from "vitest";
import { codeSymbols } from "@govlab/patterns/core/parsers/definition.parser.ts";
import { parseCode } from "@govlab/code-parse";

const JS = `
export function greet(name) {
    console.log(name);
    return name;
}
function run() {
    return greet("world");
}
`;

const ALIASED = 'import { greet as sayHi } from "./greet.ts";\nfunction run() {\n    return sayHi("world");\n}\n';

const symbolsOf = async function symbolsOf(source: string, language: string): Promise<ReturnType<typeof codeSymbols>> {
    const root = await parseCode(source, language);
    return root === null ? [] : codeSymbols(root);
};

describe("codeSymbols", () => {
    it("reads definitions and calls with their lines, marking the exported definition", async () => {
        const symbols = await symbolsOf(JS, "javascript");
        const greet = symbols.find((symbol) => symbol.role === "definition" && symbol.name === "greet");
        expect(greet?.exported).toBe(true);
        expect(greet?.callable).toBe(true);
        expect(symbols.find((symbol) => symbol.name === "run")?.exported).toBe(false);
        expect(symbols.filter((symbol) => symbol.role === "call").map((symbol) => symbol.name)).toContain("log");
        expect(symbols.every((symbol) => symbol.line > 0)).toBe(true);
    });

    it("attributes an aliased call to the imported name", async () => {
        const calls = (await symbolsOf(ALIASED, "typescript")).filter((symbol) => symbol.role === "call");
        expect(calls.map((symbol) => symbol.name)).toStrictEqual(["greet"]);
    });
});
```
