-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathwatch.spec.ts
More file actions
64 lines (50 loc) · 2.11 KB
/
watch.spec.ts
File metadata and controls
64 lines (50 loc) · 2.11 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
import * as fs from "fs-extra";
import * as path from "path";
import { forkCli } from "./run";
let testsCleanup: Array<() => void> = [];
afterEach(() => {
testsCleanup.forEach(x => x());
testsCleanup = [];
});
async function waitForFileExists(filePath: string): Promise<void> {
return new Promise<void>(resolve => {
const intervalTimerId = setInterval(() => {
if (fs.existsSync(filePath)) {
clearInterval(intervalTimerId);
resolve();
}
}, 100);
testsCleanup.push(() => clearInterval(intervalTimerId));
});
}
function forkWatchProcess(args: string[]): void {
const child = forkCli(["--watch", ...args]);
testsCleanup.push(() => child.kill());
}
const watchedFile = path.join(__dirname, "./watch/watch.ts");
const watchedFileOut = watchedFile.replace(".ts", ".lua");
afterEach(() => fs.removeSync(watchedFileOut));
async function compileChangeAndCompare(filePath: string, content: string): Promise<void> {
await waitForFileExists(watchedFileOut);
const initialResultLua = fs.readFileSync(watchedFileOut, "utf8");
fs.unlinkSync(watchedFileOut);
const originalContent = fs.readFileSync(filePath, "utf8");
fs.writeFileSync(filePath, content);
testsCleanup.push(() => fs.writeFileSync(filePath, originalContent));
await waitForFileExists(watchedFileOut);
const updatedResultLua = fs.readFileSync(watchedFileOut, "utf8");
expect(initialResultLua).not.toEqual(updatedResultLua);
}
test("should watch single file", async () => {
forkWatchProcess([path.join(__dirname, "./watch/watch.ts")]);
await compileChangeAndCompare(watchedFile, "const value = 1;");
});
test("should watch project", async () => {
forkWatchProcess(["--project", path.join(__dirname, "./watch")]);
await compileChangeAndCompare(watchedFile, "const value = 1;");
});
test("should watch config file", async () => {
const configFilePath = path.join(__dirname, "./watch/tsconfig.json");
forkWatchProcess(["--project", configFilePath]);
await compileChangeAndCompare(configFilePath, '{ "tstl": { "luaTarget": "5.3" } }');
});