-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for containerDeclarations
- Loading branch information
Showing
1 changed file
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |