Skip to content

Commit

Permalink
test: add tests for containerDeclarations
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Jan 23, 2024
1 parent 780ee52 commit 0f3ffdc
Showing 1 changed file with 203 additions and 0 deletions.
203 changes: 203 additions & 0 deletions src/container-declarations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import dedent from "ts-dedent";
import {
ModuleKind,
ModuleResolutionKind,
Project,
ScriptTarget,
} from "ts-morph";
import { expect, test } from "vitest";
import { containerDeclarations } from "./container-declarations";

test("no declarations", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
const indexFile = project.createSourceFile(
"index.ts",
dedent`
export {};
`,
);
expect(
await containerDeclarations({
containerName: "",
container: indexFile,
maxDepth: 5,
project,
}),
).toStrictEqual([]);
});

test("no declarations, no project", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
const indexFile = project.createSourceFile(
"index.ts",
dedent`
export {};
`,
);
expect(
await containerDeclarations({
containerName: "",
container: indexFile,
maxDepth: 5,
}),
).toStrictEqual([]);
});

test("multiple declarations", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
project.createSourceFile(
"file-module.ts",
dedent`
export {};
`,
);
const indexFile = project.createSourceFile(
"index.ts",
dedent`
export const fooVar: string;
export function fooFunc() {}
export class FooClass {}
export interface FooInterface {}
export enum FooEnum {}
export type FooType = {};
export namespace FooNamespace {}
export * as fooModule from './file-module';
export default 42;
`,
);
const extractedDeclarations = await containerDeclarations({
containerName: "",
container: indexFile,
maxDepth: 5,
});
expect(extractedDeclarations.length).toBe(9);
});

test("module container", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
const indexFile = project.createSourceFile(
"index.d.ts",
dedent`
declare module "foo";
`,
);

const extractedDeclarations = await containerDeclarations({
containerName: "",
container: indexFile.getModuleOrThrow('"foo"'),
maxDepth: 5,
});
expect(extractedDeclarations).toStrictEqual([]);
});

test("overloaded function", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
const indexFile = project.createSourceFile(
"index.d.ts",
dedent`
/** Docs for function overloads 1 */
declare function fooFunc(a: number): number;
/** Docs for function overloads 2 */
declare function fooFunc(a: string): string;
`,
);

const extractedDeclarations = await containerDeclarations({
containerName: "",
container: indexFile,
maxDepth: 5,
});
expect(extractedDeclarations.length).toBe(1);
});

test("function expression", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
const indexFile = project.createSourceFile(
"index.d.ts",
dedent`
export const fooFunc: (a: number) => number;
`,
);

const extractedDeclarations = await containerDeclarations({
containerName: "",
container: indexFile,
maxDepth: 5,
});
expect(extractedDeclarations.length).toBe(1);
});

test("merged namespace", async () => {
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.Bundler,
},
});
const indexFile = project.createSourceFile(
"index.ts",
dedent`
export namespace Foo {}
export namespace Foo {}
`,
);

const extractedDeclarations = await containerDeclarations({
containerName: "",
container: indexFile,
maxDepth: 5,
});
expect(extractedDeclarations.length).toBe(1);
});

0 comments on commit 0f3ffdc

Please sign in to comment.