-
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: use in memory filesystem for faster tests
- Loading branch information
Showing
13 changed files
with
422 additions
and
524 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 |
---|---|---|
@@ -1,43 +1,31 @@ | ||
import fs from "fs-extra"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import dedent from "ts-dedent"; | ||
import { Node } from "ts-morph"; | ||
import { | ||
ModuleKind, | ||
ModuleResolutionKind, | ||
Project, | ||
ScriptTarget, | ||
} from "ts-morph"; | ||
import { expect, test } from "vitest"; | ||
import { createProject } from "./create-project"; | ||
import { isClass } from "./is-class"; | ||
|
||
test("not class", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export function foo() {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("foo")?.at(0)!; | ||
expect(Node.isFunctionDeclaration(foo)).toBe(true); | ||
expect(isClass(foo)).toBe(false); | ||
test("is class", () => { | ||
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 function foo() {} | ||
test("class", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export class Foo() {} | ||
export class Foo {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("Foo")?.at(0)!; | ||
expect(Node.isClassDeclaration(foo)).toBe(true); | ||
expect(isClass(foo)).toBe(true); | ||
}); | ||
); | ||
expect(isClass(indexFile.getFunctionOrThrow("foo"))).toBe(false); | ||
expect(isClass(indexFile.getClassOrThrow("Foo"))).toBe(true); | ||
}); |
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 |
---|---|---|
@@ -1,43 +1,31 @@ | ||
import fs from "fs-extra"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import dedent from "ts-dedent"; | ||
import { Node } from "ts-morph"; | ||
import { | ||
ModuleKind, | ||
ModuleResolutionKind, | ||
Project, | ||
ScriptTarget, | ||
} from "ts-morph"; | ||
import { expect, test } from "vitest"; | ||
import { createProject } from "./create-project"; | ||
import { isEnum } from "./is-enum"; | ||
|
||
test("not enum", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export function foo() {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("foo")?.at(0)!; | ||
expect(Node.isFunctionDeclaration(foo)).toBe(true); | ||
expect(isEnum(foo)).toBe(false); | ||
test("is enum", () => { | ||
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 function foo() {} | ||
test("enum", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export enum Foo {} | ||
export enum Foo {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("Foo")?.at(0)!; | ||
expect(Node.isEnumDeclaration(foo)).toBe(true); | ||
expect(isEnum(foo)).toBe(true); | ||
}); | ||
); | ||
expect(isEnum(indexFile.getFunctionOrThrow("foo"))).toBe(false); | ||
expect(isEnum(indexFile.getEnumOrThrow("Foo"))).toBe(true); | ||
}); |
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 |
---|---|---|
@@ -1,43 +1,33 @@ | ||
import fs from "fs-extra"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import dedent from "ts-dedent"; | ||
import { Node } from "ts-morph"; | ||
import { | ||
ModuleKind, | ||
ModuleResolutionKind, | ||
Project, | ||
ScriptTarget, | ||
} from "ts-morph"; | ||
import { expect, test } from "vitest"; | ||
import { createProject } from "./create-project"; | ||
import { isExpression } from "./is-expression"; | ||
|
||
test("not expression", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export function foo() {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("foo")?.at(0)!; | ||
expect(Node.isFunctionDeclaration(foo)).toBe(true); | ||
expect(isExpression(foo)).toBe(false); | ||
test("is expression", () => { | ||
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 function foo() {} | ||
test("expression", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export default 42; | ||
export default 42; | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("default")?.at(0)!; | ||
expect(Node.isExpression(foo)).toBe(true); | ||
expect(isExpression(foo)).toBe(true); | ||
}); | ||
); | ||
expect(isExpression(indexFile.getFunctionOrThrow("foo"))).toBe(false); | ||
expect( | ||
isExpression(indexFile.getExportedDeclarations().get("default")?.at(0)!), | ||
).toBe(true); | ||
}); |
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 |
---|---|---|
@@ -1,41 +1,29 @@ | ||
import fs from "fs-extra"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import dedent from "ts-dedent"; | ||
import { Node } from "ts-morph"; | ||
import { | ||
ModuleKind, | ||
ModuleResolutionKind, | ||
Project, | ||
ScriptTarget, | ||
} from "ts-morph"; | ||
import { expect, test } from "vitest"; | ||
import { createProject } from "./create-project"; | ||
import { isFileModule } from "./is-file-module"; | ||
|
||
test("not file module", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export function foo() {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("foo")?.at(0)!; | ||
expect(Node.isFunctionDeclaration(foo)).toBe(true); | ||
expect(isFileModule(foo)).toBe(false); | ||
test("is file module", () => { | ||
const project = new Project({ | ||
useInMemoryFileSystem: true, | ||
compilerOptions: { | ||
lib: ["lib.esnext.full.d.ts"], | ||
target: ScriptTarget.ESNext, | ||
module: ModuleKind.ESNext, | ||
moduleResolution: ModuleResolutionKind.Bundler, | ||
}, | ||
}); | ||
}); | ||
|
||
test("file module", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export {}; | ||
const indexFile = project.createSourceFile( | ||
"index.ts", | ||
dedent` | ||
export function foo() {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
expect(isFileModule(indexFile)).toBe(true); | ||
}); | ||
); | ||
expect(isFileModule(indexFile.getFunctionOrThrow("foo"))).toBe(false); | ||
expect(isFileModule(indexFile)).toBe(true); | ||
}); |
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 |
---|---|---|
@@ -1,51 +1,41 @@ | ||
import fs from "fs-extra"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import dedent from "ts-dedent"; | ||
import { Node } from "ts-morph"; | ||
import { | ||
ModuleKind, | ||
ModuleResolutionKind, | ||
Project, | ||
ScriptTarget, | ||
} from "ts-morph"; | ||
import { expect, test } from "vitest"; | ||
import { createProject } from "./create-project"; | ||
import { isFunctionExpression } from "./is-function-expression"; | ||
import { isFunctionExpression } from "./api-extractor/functions"; | ||
|
||
test("not function expression", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export function foo() {} | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("foo")?.at(0)!; | ||
expect(Node.isFunctionDeclaration(foo)).toBe(true); | ||
expect(isFunctionExpression(foo)).toBe(false); | ||
test("is function expression", () => { | ||
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 function foo() {} | ||
test("function expression", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
await fs.writeFile( | ||
"./index.ts", | ||
dedent` | ||
export const foo = () => {}; | ||
export const bar = function() {} | ||
export const baz: () => void; | ||
export const bar = () => {}; | ||
export const baz = function() {}; | ||
export const qux: () => void; | ||
`, | ||
); | ||
const project = createProject("./index.ts"); | ||
expect(project.isOk()).toBe(true); | ||
const { indexFile } = project._unsafeUnwrap(); | ||
const foo = indexFile.getExportedDeclarations().get("foo")?.at(0)!; | ||
const bar = indexFile.getExportedDeclarations().get("bar")?.at(0)!; | ||
const baz = indexFile.getExportedDeclarations().get("baz")?.at(0)!; | ||
expect(Node.isVariableDeclaration(foo)).toBe(true); | ||
expect(Node.isVariableDeclaration(bar)).toBe(true); | ||
expect(Node.isVariableDeclaration(baz)).toBe(true); | ||
expect(isFunctionExpression(foo)).toBe(true); | ||
expect(isFunctionExpression(bar)).toBe(true); | ||
expect(isFunctionExpression(baz)).toBe(true); | ||
}); | ||
); | ||
expect(isFunctionExpression(indexFile.getFunctionOrThrow("foo"))).toBe(false); | ||
expect( | ||
isFunctionExpression(indexFile.getVariableDeclarationOrThrow("bar")), | ||
).toBe(true); | ||
expect( | ||
isFunctionExpression(indexFile.getVariableDeclarationOrThrow("baz")), | ||
).toBe(true); | ||
expect( | ||
isFunctionExpression(indexFile.getVariableDeclarationOrThrow("qux")), | ||
).toBe(true); | ||
}); |
Oops, something went wrong.