Skip to content

Commit 89e1f9c

Browse files
committed
chore(compas): better test abstraction
Uses pretty much no timeouts and more explicit waits.
1 parent 26a8c52 commit 89e1f9c

File tree

7 files changed

+508
-312
lines changed

7 files changed

+508
-312
lines changed

packages/compas/src/main/development/state.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ export class State {
220220
const _self = this;
221221
this.cachePersistTimer = setTimeout(() => {
222222
debugPrint("State#emitCacheUpdated :: Running cachePersist");
223-
cachePersist(_self.cache);
223+
cachePersist(_self.cache).then(() => {
224+
debugPrint("State#emitCacheUpdated :: Done with cachePersist");
225+
});
224226
}, 50);
225227
} else {
226228
this.cachePersistTimer.refresh();

packages/compas/src/shared/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export async function configResolveProjectConfig() {
125125
// @ts-expect-error
126126
throw AppError.validationError("config.resolve.parseError", {}, e);
127127
}
128+
} else {
129+
debugPrint("No config file found.");
128130
}
129131

130132
const { error, value } = validateCompasConfig(rawConfig);

packages/compas/test/cli.test.js

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { existsSync } from "node:fs";
2-
import { readdir } from "node:fs/promises";
31
import { mainTestFn, test } from "@compas/cli";
4-
import { pathJoin } from "@compas/stdlib";
5-
import { testCompasCli, testDirectory } from "./utils.js";
2+
import { isNil } from "@compas/stdlib";
3+
import { TestCompas, testDirectory } from "./utils.js";
64

75
mainTestFn(import.meta);
86

@@ -14,60 +12,69 @@ test("compas/cli", (t) => {
1412
t.test("does not create a debug file without --debug", async (t) => {
1513
const cwd = workingDirectory("no-debug");
1614

17-
await testCompasCli({
18-
args: ["foo"],
19-
inputs: [],
20-
waitForExit: true,
21-
cwd,
22-
});
15+
const cli = new TestCompas(
16+
{
17+
cwd,
18+
},
19+
{
20+
args: ["foo"],
21+
},
22+
).launch();
23+
24+
await cli.waitForExit();
25+
await cli.recalculateOutputState();
2326

24-
t.equal(existsSync(pathJoin(cwd, ".cache/compas")), false);
27+
t.ok(isNil(cli.debugFilePath));
2528
});
2629

2730
t.test("creates a debug file with --debug", async (t) => {
2831
const cwd = workingDirectory("with-debug");
2932

30-
await testCompasCli({
31-
args: ["foo", "--debug"],
32-
inputs: [],
33-
waitForExit: true,
34-
cwd,
35-
});
33+
const cli = new TestCompas(
34+
{
35+
cwd,
36+
},
37+
{
38+
args: ["foo", "--debug"],
39+
},
40+
).launch();
3641

37-
t.equal(existsSync(pathJoin(cwd, ".cache/compas")), true);
38-
t.equal(
39-
(await readdir(pathJoin(cwd, ".cache/compas"), {})).some(
40-
(it) => it.startsWith("debug-") && it.endsWith(".txt"),
41-
),
42-
true,
43-
);
42+
await cli.waitForExit();
43+
await cli.recalculateOutputState();
44+
45+
t.ok(cli.debugFilePath);
4446
});
4547

4648
t.test("package.json is not available", async (t) => {
4749
const cwd = workingDirectory("no-package-json");
4850

49-
const { stdout } = await testCompasCli({
50-
args: [],
51-
inputs: [],
52-
waitForExit: true,
51+
const cli = new TestCompas({
5352
cwd,
54-
});
53+
}).launch();
54+
55+
await cli.waitForExit();
5556

5657
t.ok(
57-
stdout.includes("Please run 'npx compas@latest init' to install Compas."),
58+
cli.stdout.includes(
59+
"Please run 'npx compas@latest init' to install Compas.",
60+
),
5861
);
5962
});
6063

6164
t.test("unsupported command", async (t) => {
6265
const cwd = workingDirectory("unknown-command");
6366

64-
const { stdout } = await testCompasCli({
65-
args: ["foo"],
66-
inputs: [],
67-
waitForExit: true,
68-
cwd,
69-
});
67+
const cli = new TestCompas(
68+
{
69+
cwd,
70+
},
71+
{
72+
args: ["foo"],
73+
},
74+
).launch();
75+
76+
await cli.waitForExit();
7077

71-
t.ok(stdout.includes(`Unsupported command. Available commands:`));
78+
t.ok(cli.stdout.includes("Unsupported command. Available commands:"));
7279
});
7380
});

0 commit comments

Comments
 (0)