-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtypescript-tools.integration.test.ts
More file actions
148 lines (129 loc) · 4.08 KB
/
Copy pathtypescript-tools.integration.test.ts
File metadata and controls
148 lines (129 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { fileURLToPath } from "node:url";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
import {
NodeRuntime,
allowAllFs,
createInMemoryFileSystem,
createNodeDriver,
createNodeRuntimeDriverFactory,
} from "secure-exec";
import { createTypeScriptTools } from "../src/index.js";
const workspaceRoot = resolve(fileURLToPath(new URL("../../..", import.meta.url)));
function createTools(memoryLimit?: number) {
const filesystem = createInMemoryFileSystem();
return {
filesystem,
tools: createTypeScriptTools({
systemDriver: createNodeDriver({
filesystem,
moduleAccess: { cwd: workspaceRoot },
permissions: allowAllFs,
}),
runtimeDriverFactory: createNodeRuntimeDriverFactory(),
memoryLimit,
}),
};
}
describe("@secure-exec/typescript", () => {
it("typechecks a project with node types from node_modules", { timeout: 15_000 }, async () => {
const { filesystem, tools } = createTools();
await filesystem.mkdir("/root/src");
await filesystem.writeFile(
"/root/tsconfig.json",
JSON.stringify({
compilerOptions: {
module: "nodenext",
moduleResolution: "nodenext",
target: "es2022",
types: ["node"],
skipLibCheck: true,
},
include: ["src/**/*.ts"],
}),
);
await filesystem.writeFile(
"/root/src/index.ts",
'import { Buffer } from "node:buffer";\nexport const output: Buffer = Buffer.from("ok");\n',
);
const result = await tools.typecheckProject({ cwd: "/root" });
expect(result.success).toBe(true);
expect(result.diagnostics).toEqual([]);
});
it("compiles a project into the virtual filesystem and the output executes in NodeRuntime", async () => {
const { filesystem, tools } = createTools();
await filesystem.mkdir("/root/src");
await filesystem.writeFile(
"/root/tsconfig.json",
JSON.stringify({
compilerOptions: {
module: "commonjs",
target: "es2022",
outDir: "/root/dist",
},
include: ["src/**/*.ts"],
}),
);
await filesystem.writeFile(
"/root/src/index.ts",
"export const value: number = 7;\n",
);
const compileResult = await tools.compileProject({ cwd: "/root" });
expect(compileResult.success).toBe(true);
expect(compileResult.emitSkipped).toBe(false);
expect(compileResult.emittedFiles).toContain("/root/dist/index.js");
const emitted = await filesystem.readTextFile("/root/dist/index.js");
expect(emitted).toContain("exports.value = 7");
const runtime = new NodeRuntime({
systemDriver: createNodeDriver({
filesystem,
moduleAccess: { cwd: workspaceRoot },
permissions: allowAllFs,
}),
runtimeDriverFactory: createNodeRuntimeDriverFactory(),
});
const execution = await runtime.run("module.exports = require('./dist/index.js');", "/root/index.js");
runtime.dispose();
expect(execution.code).toBe(0);
expect(execution.exports).toEqual({ value: 7 });
});
it("typechecks a source string without mutating the filesystem", async () => {
const { tools } = createTools();
const result = await tools.typecheckSource({
sourceText: "const value: string = 1;\n",
filePath: "/root/input.ts",
});
expect(result.success).toBe(false);
expect(result.diagnostics.some((diagnostic) => diagnostic.code === 2322)).toBe(
true,
);
});
it("compiles a source string to JavaScript text", async () => {
const { tools } = createTools();
const result = await tools.compileSource({
sourceText: "export const value: number = 3;\n",
filePath: "/root/input.ts",
compilerOptions: {
module: "commonjs",
target: "es2022",
},
});
expect(result.success).toBe(true);
expect(result.outputText).toContain("exports.value = 3");
});
it("returns a deterministic failure when the compiler isolate exceeds its memory limit", async () => {
const { tools } = createTools(64);
const result = await tools.typecheckSource({
sourceText: "export const value = 1;\n",
filePath: "/root/input.ts",
});
expect(result.success).toBe(false);
expect(result.diagnostics).toEqual([
expect.objectContaining({
category: "error",
code: 0,
message: expect.any(String),
}),
]);
});
});