Skip to content

Build your personal knowledge base with Trilium Notes

test: hoist vi.mock() calls to module scope

Elian Doraneed968471031
diff --git a/apps/client/src/widgets/ribbon/FormattingToolbar.spec.ts b/apps/client/src/widgets/ribbon/FormattingToolbar.spec.ts
index f35a976090..557a390052 100644
--- a/apps/client/src/widgets/ribbon/FormattingToolbar.spec.ts
+++ b/apps/client/src/widgets/ribbon/FormattingToolbar.spec.ts
@@ -7,6 +7,20 @@ import { randomString } from "../../services/utils";
import { buildNote } from "../../test/easy-froca";
import { getFormattingToolbarState } from "./FormattingToolbar";
+vi.mock("../../services/tree.ts", () => ({
+ default: {
+ getActiveContextNotePath() {
+ return "root";
+ },
+ resolveNotePath(inputNotePath: string) {
+ return inputNotePath;
+ },
+ getNoteIdFromUrl(url) {
+ return url.split("/").at(-1);
+ }
+ }
+}));
+
interface NoteContextInfo {
type: NoteType;
viewScope?: ViewMode;
@@ -15,20 +29,6 @@ interface NoteContextInfo {
describe("Formatting toolbar logic", () => {
beforeAll(() => {
- vi.mock("../../services/tree.ts", () => ({
- default: {
- getActiveContextNotePath() {
- return "root";
- },
- resolveNotePath(inputNotePath: string) {
- return inputNotePath;
- },
- getNoteIdFromUrl(url) {
- return url.split("/").at(-1);
- }
- }
- }));
-
buildNote({
id: "root",
title: "Root"
diff --git a/apps/server/src/routes/api/clipper.spec.ts b/apps/server/src/routes/api/clipper.spec.ts
index 7c637daf4c..f2e19834d5 100644
--- a/apps/server/src/routes/api/clipper.spec.ts
+++ b/apps/server/src/routes/api/clipper.spec.ts
@@ -6,6 +6,20 @@ import clipperRoute, { processContent } from "./clipper";
const { buildNote } = becca_easy_mocking;
+vi.mock("../../services/image.js", () => ({
+ default: {
+ saveImageToAttachment() {
+ return {
+ attachmentId: "foo",
+ title: "encodedTitle",
+ };
+ },
+ // The clipping is read as soon as this answers, so the picture has to be stored by
+ // then; nothing here defers, so there is nothing for the wait to do.
+ awaitImageWrite: async () => {}
+ }
+}));
+
let note!: BNote;
describe("processContent", () => {
@@ -14,19 +28,6 @@ describe("processContent", () => {
content: "Hi there"
});
note.saveAttachment = () => {};
- vi.mock("../../services/image.js", () => ({
- default: {
- saveImageToAttachment() {
- return {
- attachmentId: "foo",
- title: "encodedTitle",
- };
- },
- // The clipping is read as soon as this answers, so the picture has to be stored by
- // then; nothing here defers, so there is nothing for the wait to do.
- awaitImageWrite: async () => {}
- }
- }));
});
it("processes basic note", async () => {
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.ts
index b3415ab5c9..04a4744b7a 100644
--- a/apps/server/src/share/content_renderer.spec.ts
+++ b/apps/server/src/share/content_renderer.spec.ts
@@ -2,7 +2,7 @@ import { trimIndentation } from "@triliumnext/commons";
import { sanitize, utils } from "@triliumnext/core";
import ejs from "ejs";
import { parse } from "node-html-parser";
-import { beforeAll, describe, expect, it, vi } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { buildShareNote, buildShareNotes } from "../test/shaca_mocking.js";
import { getContent, getDefaultTemplatePath, readTemplate, renderCode, renderNoteContent, type Result, shouldSyntaxHighlight } from "./content_renderer.js";
@@ -10,16 +10,14 @@ import type SNote from "./shaca/entities/snote.js";
import shaca from "./shaca/shaca.js";
import shareRoot from "./share_root.js";
-describe("content_renderer", () => {
- beforeAll(() => {
- vi.mock("../becca/becca_loader.js", () => ({
- default: {
- load: vi.fn(),
- loaded: Promise.resolve()
- }
- }));
- });
+vi.mock("../becca/becca_loader.js", () => ({
+ default: {
+ load: vi.fn(),
+ loaded: Promise.resolve()
+ }
+}));
+describe("content_renderer", () => {
it("Reports protected notes not being renderable", () => {
const note = buildShareNote({ isProtected: true });
const result = getContent(note);
diff --git a/packages/trilium-core/src/services/script.spec.ts b/packages/trilium-core/src/services/script.spec.ts
index ebf497cdfe..170d63a4b6 100644
--- a/packages/trilium-core/src/services/script.spec.ts
+++ b/packages/trilium-core/src/services/script.spec.ts
@@ -8,6 +8,27 @@ import { getContext } from "./context.js";
import scriptService, { buildJsx, executeBundle, getScriptBundle } from "./script.js";
import ws from "./ws.js";
+vi.mock("./sql.js", () => {
+ return {
+ default: {
+ transactional: (cb: Function) => {
+ cb();
+ },
+ execute: () => {},
+ replace: () => {},
+ getMap: () => {}
+ }
+ };
+});
+
+vi.mock("./sql_init.js", () => {
+ const mock = {
+ initializeDb: () => {},
+ dbReady: Promise.resolve()
+ };
+ return { default: mock, ...mock };
+});
+
describe("Script", () => {
// executeBundle enforces the backendScriptingEnabled security toggle (default false).
const originalScriptingEnabled = config.Security.backendScriptingEnabled;
@@ -26,27 +47,6 @@ describe("Script", () => {
vi.spyOn(ws, "sendMessageToAllClients").mockImplementation(() => {}).mockClear();
buildNote({ id: "root", title: "root" });
-
- vi.mock("./sql.js", () => {
- return {
- default: {
- transactional: (cb: Function) => {
- cb();
- },
- execute: () => {},
- replace: () => {},
- getMap: () => {}
- }
- };
- });
-
- vi.mock("./sql_init.js", () => {
- const mock = {
- initializeDb: () => {},
- dbReady: Promise.resolve()
- };
- return { default: mock, ...mock };
- });
});
it("returns result from script", () => {
diff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.ts
index 15f55bb794..363edfa530 100644
--- a/packages/trilium-core/src/services/tree.spec.ts
+++ b/packages/trilium-core/src/services/tree.spec.ts
@@ -7,6 +7,27 @@ import tree from "./tree.js";
import {buildNote} from "../test/becca_easy_mocking.js";
import { getContext } from "./context.js";
+vi.mock("./sql.js", () => {
+ return {
+ default: {
+ transactional: (cb: Function) => {
+ cb();
+ },
+ execute: () => {},
+ replace: () => {},
+ getMap: () => {}
+ }
+ };
+});
+
+vi.mock("./sql_init.js", () => {
+ const mock = {
+ initializeDb: () => {},
+ dbReady: Promise.resolve()
+ };
+ return { default: mock, ...mock };
+});
+
describe("Tree", () => {
let rootNote!: NoteBuilder;
@@ -26,27 +47,6 @@ describe("Tree", () => {
parentNoteId: "none",
notePosition: 10
});
-
- vi.mock("./sql.js", () => {
- return {
- default: {
- transactional: (cb: Function) => {
- cb();
- },
- execute: () => {},
- replace: () => {},
- getMap: () => {}
- }
- };
- });
-
- vi.mock("./sql_init.js", () => {
- const mock = {
- initializeDb: () => {},
- dbReady: Promise.resolve()
- };
- return { default: mock, ...mock };
- });
});
it("sorts notes by title (base case)", () => {