Skip to content
This repository was archived by the owner on Sep 19, 2025. It is now read-only.

Commit 011d0b5

Browse files
committed
Create test helper
1 parent 22ab1fc commit 011d0b5

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/autocomplete/getAutocompletion.test.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
import { describe, expect, it } from "vitest";
22
import { getAutocompletion } from "./getAutocompletion.js";
3-
import {
4-
createSystem,
5-
createVirtualTypeScriptEnvironment,
6-
} from "@typescript/vfs";
7-
import ts from "typescript";
8-
import * as Fs from "node:fs";
9-
10-
const fsMap = new Map<string, string>(
11-
JSON.parse(
12-
Fs.readFileSync(new URL("../../test/cdn.json", import.meta.url), "utf8"),
13-
),
14-
);
15-
16-
const system = createSystem(fsMap);
3+
import { getEnv } from "../../test/env.js";
174

185
describe("getAutocompletion", () => {
196
it("null", async () => {
20-
const env = createVirtualTypeScriptEnvironment(system, [], ts, {
21-
lib: ["es2022"],
22-
});
7+
const env = getEnv();
238

249
await expect(
2510
getAutocompletion({
@@ -34,9 +19,7 @@ describe("getAutocompletion", () => {
3419
});
3520

3621
it("completion", async () => {
37-
const env = createVirtualTypeScriptEnvironment(system, [], ts, {
38-
lib: ["es2022"],
39-
});
22+
const env = getEnv();
4023

4124
const name = "/foo.ts";
4225
const content = "const x = 'hi'.";

src/autocomplete/tsAutocomplete.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,35 @@ import { describe, it, expect } from "vitest";
22
import { tsAutocomplete } from "./tsAutocomplete.js";
33
import { EditorState } from "@codemirror/state";
44
import { CompletionContext } from "@codemirror/autocomplete";
5+
import { tsFacet } from "../facet/tsFacet.js";
6+
import { getEnv } from "../../test/env.js";
57

68
describe("tsAutocomplete", () => {
79
it("null", async () => {
810
const a = tsAutocomplete();
911
const state = EditorState.create();
1012
await expect(a(new CompletionContext(state, 0, true))).resolves.toBeNull();
1113
});
14+
15+
it("getting completions successfully", async () => {
16+
const a = tsAutocomplete();
17+
const doc = "export const x = 10.";
18+
const env = getEnv();
19+
20+
const path = "/foo.ts";
21+
const content = "const x = 'hi'.";
22+
env.createFile(path, content);
23+
24+
const state = EditorState.create({
25+
doc,
26+
extensions: [tsFacet.of({ env, path })],
27+
});
28+
29+
const completions = await a(new CompletionContext(state, doc.length, true));
30+
31+
expect(completions).toMatchObject({
32+
from: doc.length,
33+
options: expect.any(Array),
34+
});
35+
});
1236
});

test/env.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
createSystem,
3+
createVirtualTypeScriptEnvironment,
4+
} from "@typescript/vfs";
5+
import ts from "typescript";
6+
import * as Fs from "node:fs";
7+
8+
const fsMap = new Map<string, string>(
9+
JSON.parse(Fs.readFileSync(new URL("./cdn.json", import.meta.url), "utf8")),
10+
);
11+
12+
const system = createSystem(fsMap);
13+
14+
export function getEnv() {
15+
return createVirtualTypeScriptEnvironment(system, [], ts, {
16+
lib: ["es2022"],
17+
});
18+
}

0 commit comments

Comments
 (0)