Skip to content

Commit a119b71

Browse files
authored
Migrarte final api/node tests to Vitest (#10114)
1 parent 70e102e commit a119b71

File tree

6 files changed

+475
-1139
lines changed

6 files changed

+475
-1139
lines changed

api/node/__test__/eventloop.spec.mts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
// Test that the Slint event loop processes libuv's events.
55

6-
import test from "ava";
6+
import { test, expect, afterEach } from "vitest";
77
import * as http from "node:http";
88

99
import { runEventLoop, quitEventLoop, private_api } from "../dist/index.js";
1010

11-
test.serial("merged event loops with timer", async (t) => {
11+
afterEach(() => {
12+
quitEventLoop();
13+
});
14+
15+
test.sequential("merged event loops with timer", async () => {
1216
let invoked = false;
1317

1418
await runEventLoop(() => {
@@ -17,10 +21,10 @@ test.serial("merged event loops with timer", async (t) => {
1721
quitEventLoop();
1822
}, 2);
1923
});
20-
t.true(invoked);
24+
expect(invoked).toBe(true);
2125
});
2226

23-
test.serial("merged event loops with networking", async (t) => {
27+
test.sequential("merged event loops with networking", async () => {
2428
const listener = (request, result) => {
2529
result.writeHead(200);
2630
result.end("Hello World");
@@ -48,32 +52,29 @@ test.serial("merged event loops with networking", async (t) => {
4852
});
4953
});
5054

51-
t.is(received_response, "Hello World");
55+
expect(received_response).toBe("Hello World");
5256
});
5357

54-
test.serial(
55-
"quit event loop on last window closed with callback",
56-
async (t) => {
57-
const compiler = new private_api.ComponentCompiler();
58-
const definition = compiler.buildFromSource(
59-
`
58+
test.sequential("quit event loop on last window closed with callback", async () => {
59+
const compiler = new private_api.ComponentCompiler();
60+
const definition = compiler.buildFromSource(
61+
`
6062
6163
export component App inherits Window {
6264
width: 300px;
6365
height: 300px;
6466
}`,
65-
"",
66-
);
67-
t.not(definition.App, null);
67+
"",
68+
);
69+
expect(definition.App).not.toBeNull();
6870

69-
const instance = definition.App!.create() as any;
70-
t.not(instance, null);
71+
const instance = definition.App!.create() as any;
72+
expect(instance).not.toBeNull();
7173

72-
instance.window().show();
73-
await runEventLoop(() => {
74-
setTimeout(() => {
75-
instance.window().hide();
76-
}, 2);
77-
});
78-
},
79-
);
74+
instance.window().show();
75+
await runEventLoop(() => {
76+
setTimeout(() => {
77+
instance.window().hide();
78+
}, 2);
79+
});
80+
});

api/node/__test__/helpers/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright © SixtyFPS GmbH <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3+
4+
import { hook } from "capture-console";
5+
6+
export function captureAsyncStderr() {
7+
const chunks: string[] = [];
8+
9+
const streams = new Set<NodeJS.WritableStream>();
10+
streams.add(process.stderr);
11+
12+
const consoleStderr = (globalThis.console as any)?._stderr;
13+
if (consoleStderr && consoleStderr !== process.stderr) {
14+
streams.add(consoleStderr);
15+
}
16+
17+
const unhooks = Array.from(streams).map((stream) =>
18+
hook(stream, { quiet: true }, (chunk) => {
19+
chunks.push(chunk);
20+
}),
21+
);
22+
23+
return {
24+
output() {
25+
return chunks.join("");
26+
},
27+
restore() {
28+
while (unhooks.length) {
29+
const unhook = unhooks.pop();
30+
unhook && unhook();
31+
}
32+
},
33+
};
34+
}

0 commit comments

Comments
 (0)