Skip to content

Commit a6df646

Browse files
committed
Properly fix stderr test
1 parent 498e595 commit a6df646

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

api/node/__test__/helpers/utils.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 { Console } from "node:console";
5+
import { Writable } from "node:stream";
6+
import stripAnsi from "strip-ansi";
7+
8+
export function captureLogs() {
9+
const stdout: string[] = [];
10+
const stderr: string[] = [];
11+
12+
const streams = {
13+
stdout: new Writable({
14+
write(chunk, _encoding, callback) {
15+
stdout.push(chunk.toString());
16+
callback();
17+
},
18+
}),
19+
stderr: new Writable({
20+
write(chunk, _encoding, callback) {
21+
stderr.push(chunk.toString());
22+
callback();
23+
},
24+
}),
25+
};
26+
27+
const originalConsole = globalThis.console;
28+
globalThis.console = new Console({
29+
stdout: streams.stdout,
30+
stderr: streams.stderr,
31+
});
32+
33+
const originalStdoutWrite = process.stdout.write;
34+
process.stdout.write = streams.stdout.write.bind(streams.stdout) as any;
35+
36+
const originalStderrWrite = process.stderr.write;
37+
process.stderr.write = streams.stderr.write.bind(streams.stderr) as any;
38+
39+
return {
40+
restore() {
41+
globalThis.console = originalConsole;
42+
process.stdout.write = originalStdoutWrite;
43+
process.stderr.write = originalStderrWrite;
44+
},
45+
getLogs() {
46+
return {
47+
stdout: stripAnsi(stdout.join("")),
48+
stderr: stripAnsi(stderr.join("")),
49+
};
50+
},
51+
};
52+
}

api/node/__test__/js_value_conversion.spec.mts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { test, expect } from "vitest";
55
import * as path from "node:path";
66
import { fileURLToPath } from "node:url";
77
import { Jimp } from "jimp";
8-
import { captureStderr } from "capture-console";
9-
8+
import { captureLogs } from "./helpers/utils.js";
109
import {
1110
private_api,
1211
type ImageData,
@@ -1049,7 +1048,7 @@ test("wrong global callback return type ", () => {
10491048
expect(person).toStrictEqual({ name: "", age: 0 });
10501049
});
10511050

1052-
test("throw exception in callback", () => {
1051+
test("throw exception in callback", async () => {
10531052
const compiler = new private_api.ComponentCompiler();
10541053
const definition = compiler.buildFromSource(
10551054
`
@@ -1069,22 +1068,18 @@ test("throw exception in callback", () => {
10691068
throw new Error("I'm an error");
10701069
});
10711070

1072-
const output = captureStderr(() => {
1071+
const logs = captureLogs();
1072+
try {
10731073
instance!.invoke("throw-something", []);
1074-
});
1075-
1076-
if (output) {
1077-
expect(
1078-
output.includes(
1079-
"Node.js: Invoking callback 'throw-something' failed",
1080-
),
1081-
).toBe(true);
1082-
expect(output.includes("I'm an error")).toBe(true);
1083-
} else {
1084-
// If captureStderr doesn't work in Vitest, we at least verify the callback was invoked
1085-
// The error will be visible in the test output
1086-
expect(true).toBe(true);
1074+
await new Promise((resolve) => setTimeout(resolve, 0));
1075+
} finally {
1076+
logs.restore();
10871077
}
1078+
const output = logs.getLogs().stderr;
1079+
expect(
1080+
output.includes("Node.js: Invoking callback 'throw-something' failed"),
1081+
).toBe(true);
1082+
expect(output.includes("I'm an error")).toBe(true);
10881083
});
10891084

10901085
test("throw exception set color", () => {

api/node/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"format:fix": "biome format --write",
4949
"lint": "biome lint",
5050
"lint:fix": "biome lint --fix",
51-
"test": "tsc --build __test__/tsconfig.json && (ava || true) && vitest run"
51+
"test": "vitest run"
5252
},
5353
"ava": {
5454
"typescript": {
@@ -75,6 +75,7 @@
7575
]
7676
},
7777
"dependencies": {
78-
"@napi-rs/cli": "2.18.4"
78+
"@napi-rs/cli": "2.18.4",
79+
"strip-ansi": "7.1.2"
7980
}
8081
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)