Skip to content

Build your personal knowledge base with Trilium Notes

fix(tests/standalone): keep core specs passing under happy-dom

Elian Doraneb5e4fdd3744
diff --git a/apps/client-standalone/src/test_setup.ts b/apps/client-standalone/src/test_setup.ts
index 08f26d0278..08e73f1116 100644
--- a/apps/client-standalone/src/test_setup.ts
+++ b/apps/client-standalone/src/test_setup.ts
@@ -4,7 +4,6 @@ import { fileURLToPath } from "node:url";
import { initializeCore, options } from "@triliumnext/core";
import schemaSql from "@triliumnext/core/src/assets/schema.sql?raw";
-import HappyDomHtmlParser from "happy-dom/lib/html-parser/HTMLParser.js";
import serverEnTranslations from "../../server/src/assets/translations/en/server.json";
import { beforeAll } from "vitest";
@@ -71,23 +70,17 @@ WebAssembly.instantiateStreaming = (async (source, importObject) => {
// Per HTML5 parsing spec, a single U+000A LINE FEED immediately after a <pre>,
// <listing>, or <textarea> start tag must be ignored ("newlines at the start
// of pre blocks are ignored as an authoring convenience"). Real browsers and
-// domino (which the server runtime uses via turnish) both implement this;
-// happy-dom (as of 20.8.9) does not — it keeps the LF as a text node.
-//
-// That difference makes turnish's markdown export produce different output
-// under happy-dom vs. production, breaking markdown.spec.ts > "exports jQuery
-// code in table properly". Patch HTMLParser.parse to pre-process the string.
+// domino (which turnish uses in Node) both implement this; happy-dom does not.
+// Patch at the DOMParser boundary since turnish prefers DOMParser when it's
+// available — patching via module-level HTMLParser import hits a different
+// happy-dom copy than the vitest env loaded.
const LEADING_LF_IN_PRE_RE = /(<(?:pre|listing|textarea)\b[^>]*>)(\r\n|\r|\n)/gi;
-const originalHtmlParserParse = (HappyDomHtmlParser as unknown as {
- prototype: { parse(html: string, rootNode?: unknown): unknown };
-}).prototype.parse;
-(HappyDomHtmlParser as unknown as {
- prototype: { parse(html: string, rootNode?: unknown): unknown };
-}).prototype.parse = function (html: string, rootNode?: unknown) {
- const patched = typeof html === "string"
- ? html.replace(LEADING_LF_IN_PRE_RE, "$1")
- : html;
- return originalHtmlParserParse.call(this, patched, rootNode);
+const originalParseFromString = DOMParser.prototype.parseFromString;
+DOMParser.prototype.parseFromString = function (source: string, type: DOMParserSupportedType) {
+ const patched = typeof source === "string"
+ ? source.replace(LEADING_LF_IN_PRE_RE, "$1")
+ : source;
+ return originalParseFromString.call(this, patched, type);
};
// =============================================================================
diff --git a/packages/trilium-core/src/services/import/enex.spec.ts b/packages/trilium-core/src/services/import/enex.spec.ts
index d66494778c..8f8610d75b 100644
--- a/packages/trilium-core/src/services/import/enex.spec.ts
+++ b/packages/trilium-core/src/services/import/enex.spec.ts
@@ -8,6 +8,7 @@ import type BNote from "../../becca/entities/bnote.js";
import { getContext } from "../context.js";
import sql_init from "../sql_init.js";
import TaskContext from "../task_context.js";
+import { decodeUtf8 } from "../utils/binary.js";
import enex from "./enex.js";
const scriptDir = dirname(fileURLToPath(import.meta.url));
@@ -61,15 +62,15 @@ describe("importEnex", () => {
const txt = attachments.find(a => a.title === "attachments1.txt");
expect(txt).toBeTruthy();
expect(txt!.mime).toBe("text/plain");
- expect(txt!.getContent().toString()).toBe("111");
+ expect(decodeUtf8(txt!.getContent())).toBe("111");
const bin = attachments.find(a => a.title === "attachments2");
expect(bin).toBeTruthy();
expect(bin!.mime).toBe("application/octet-stream");
- expect(bin!.getContent().toString()).toBe("222");
+ expect(decodeUtf8(bin!.getContent())).toBe("222");
// The note content should contain reference links to the attachments
- const content = test1!.getContent().toString();
+ const content = decodeUtf8(test1!.getContent());
expect(content).toContain(`class="reference-link" href="#root/${test1!.noteId}?viewMode=attachments&amp;attachmentId=${txt!.attachmentId}"`);
expect(content).toContain(`class="reference-link" href="#root/${test1!.noteId}?viewMode=attachments&amp;attachmentId=${bin!.attachmentId}"`);
});