Skip to content

Commit 8b16084

Browse files
committed
Go back to capture-console
1 parent 2664f16 commit 8b16084

File tree

4 files changed

+26
-50
lines changed

4 files changed

+26
-50
lines changed

api/node/__test__/helpers/utils.ts

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,34 @@
11
// Copyright © SixtyFPS GmbH <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
33

4-
import { Console } from "node:console";
5-
import { Writable } from "node:stream";
6-
import stripAnsi from "strip-ansi";
4+
import { hook } from "capture-console";
75

8-
export function captureLogs() {
9-
const stdout: string[] = [];
10-
const stderr: string[] = [];
6+
export function captureAsyncStderr() {
7+
const chunks: string[] = [];
118

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-
});
9+
const streams = new Set<NodeJS.WritableStream>();
10+
streams.add(process.stderr);
3211

33-
const originalStdoutWrite = process.stdout.write;
34-
process.stdout.write = streams.stdout.write.bind(streams.stdout) as any;
12+
const consoleStderr = (globalThis.console as any)?._stderr;
13+
if (consoleStderr && consoleStderr !== process.stderr) {
14+
streams.add(consoleStderr);
15+
}
3516

36-
const originalStderrWrite = process.stderr.write;
37-
process.stderr.write = streams.stderr.write.bind(streams.stderr) as any;
17+
const unhooks = Array.from(streams).map((stream) =>
18+
hook(stream, { quiet: true }, (chunk) => {
19+
chunks.push(chunk);
20+
}),
21+
);
3822

3923
return {
40-
restore() {
41-
globalThis.console = originalConsole;
42-
process.stdout.write = originalStdoutWrite;
43-
process.stderr.write = originalStderrWrite;
24+
output() {
25+
return chunks.join("");
4426
},
45-
getLogs() {
46-
return {
47-
stdout: stripAnsi(stdout.join("")),
48-
stderr: stripAnsi(stderr.join("")),
49-
};
27+
restore() {
28+
while (unhooks.length) {
29+
const unhook = unhooks.pop();
30+
unhook && unhook();
31+
}
5032
},
5133
};
5234
}

api/node/__test__/js_value_conversion.spec.mts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +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 { captureLogs } from "./helpers/utils.js";
8+
import { captureAsyncStderr } from "./helpers/utils.js";
99
import {
1010
private_api,
1111
type ImageData,
@@ -1062,23 +1062,21 @@ test("throw exception in callback", async () => {
10621062

10631063
const instance = definition.App!.create();
10641064
expect(instance).not.toBeNull();
1065-
let speakTest: string;
10661065

10671066
instance!.setCallback("throw-something", () => {
10681067
throw new Error("I'm an error");
10691068
});
10701069

1071-
const logs = captureLogs();
1070+
const stderrCapture = captureAsyncStderr();
10721071
try {
10731072
instance!.invoke("throw-something", []);
10741073
// Vitest runs these tests in workers and the native binding writes to
10751074
// stderr on the next macrotask, so yield once before restoring writers.
1076-
// https://github.com/vitest-dev/vitest/discussions/5366
10771075
await new Promise((resolve) => setTimeout(resolve, 0));
10781076
} finally {
1079-
logs.restore();
1077+
stderrCapture.restore();
10801078
}
1081-
const output = logs.getLogs().stderr;
1079+
const output = stderrCapture.output();
10821080
expect(
10831081
output.includes("Node.js: Invoking callback 'throw-something' failed"),
10841082
).toBe(true);

api/node/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"test": "vitest run"
5050
},
5151
"dependencies": {
52-
"@napi-rs/cli": "2.18.4",
53-
"strip-ansi": "7.1.2"
52+
"@napi-rs/cli": "2.18.4"
5453
}
5554
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)