Skip to content

Build your personal knowledge base with Trilium Notes

test(server): stop mirroring the log to the console under vitest

Elian Doran9bd59e0fed00
diff --git a/apps/server/src/log_provider.spec.ts b/apps/server/src/log_provider.spec.ts
new file mode 100644
index 0000000000..0fa47a5d42
--- /dev/null
+++ b/apps/server/src/log_provider.spec.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it, vi } from "vitest";
+
+import ServerLogService from "./log_provider.js";
+
+describe("ServerLogService console echo", () => {
+ it("writes the entry to the log file but does not echo it under vitest", () => {
+ // Every console call in a vitest worker crosses the RPC boundary to the main process. This
+ // suite emits tens of thousands of log lines, and one still in flight when a worker tears
+ // down fails the whole run with `Closing rpc while "onUserConsoleLog" was pending` even
+ // though every test passed — so the echo is suppressed while the file write is kept.
+ const log = new ServerLogService();
+ const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
+ const writes: string[] = [];
+ const writeEntry = vi
+ .spyOn(log as unknown as { writeEntry(entry: string): void }, "writeEntry")
+ .mockImplementation((entry: string) => void writes.push(entry));
+
+ try {
+ expect(process.env.VITEST).toBeTruthy();
+
+ log.info("hidden subtree bootstrap chatter");
+
+ expect(consoleLog).not.toHaveBeenCalled();
+ expect(writes.join("")).toContain("hidden subtree bootstrap chatter");
+ } finally {
+ writeEntry.mockRestore();
+ consoleLog.mockRestore();
+ }
+ });
+
+ it("echoes to the console outside vitest, so container logs still show activity", () => {
+ const log = new ServerLogService();
+ const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
+ const writeEntry = vi
+ .spyOn(log as unknown as { writeEntry(entry: string): void }, "writeEntry")
+ .mockImplementation(() => {});
+ const previous = process.env.VITEST;
+
+ try {
+ delete process.env.VITEST;
+ log.info("server started");
+
+ expect(consoleLog).toHaveBeenCalledWith("server started");
+ } finally {
+ if (previous !== undefined) {
+ process.env.VITEST = previous;
+ }
+ writeEntry.mockRestore();
+ consoleLog.mockRestore();
+ }
+ });
+});
diff --git a/apps/server/src/log_provider.ts b/apps/server/src/log_provider.ts
index 9e5acefcb0..cbe0961238 100644
--- a/apps/server/src/log_provider.ts
+++ b/apps/server/src/log_provider.ts
@@ -54,6 +54,20 @@ export default class ServerLogService extends FileBasedLogService {
this.logFile?.write(entry);
}
+ protected override echoToConsole(str: string): void {
+ // Under vitest every console call is forwarded to the main process over the worker RPC, and
+ // this suite emits tens of thousands of them — the hidden-subtree bootstrap alone logs a
+ // line per created note, for each of the ~60 spec files that build the app. A call still in
+ // flight when its worker tears down surfaces as an unhandled
+ // `EnvironmentTeardownError: Closing rpc while "onUserConsoleLog" was pending`, which fails
+ // the run even though every test passed. The entry is still written to the log file, so
+ // nothing that reads the log is affected.
+ if (process.env.VITEST) {
+ return;
+ }
+ super.echoToConsole(str);
+ }
+
protected override readLogFile(fileName: string): string | null {
const filePath = path.join(dataDir.LOG_DIR, fileName);
try {
diff --git a/packages/trilium-core/src/services/file_based_log.ts b/packages/trilium-core/src/services/file_based_log.ts
index 8440a27836..8a10c4a7f7 100644
--- a/packages/trilium-core/src/services/file_based_log.ts
+++ b/packages/trilium-core/src/services/file_based_log.ts
@@ -168,6 +168,15 @@ export default abstract class FileBasedLogService extends LogService {
const entry = `${this.formatTime(millisSinceMidnight)} ${str}${this.eol}`;
this.writeEntry(entry);
+ this.echoToConsole(str);
+ }
+
+ /**
+ * Mirrors an entry to stdout, which is how `docker logs`/journalctl surface server activity.
+ * Overridable so a runtime that captures stdout through a costly channel can opt out while
+ * still writing the entry to the log file.
+ */
+ protected echoToConsole(str: string): void {
console.log(str);
}