Skip to content

Build your personal knowledge base with Trilium Notes

refactor(test): global initialisation

Elian Doranf646e0f72458
diff --git a/src/public/app/test-setup.ts b/src/public/app/test-setup.ts
new file mode 100644
index 0000000000..75ca3ae6e5
--- /dev/null
+++ b/src/public/app/test-setup.ts
@@ -0,0 +1,51 @@
+import { beforeAll, vi } from "vitest";
+import $ from "jquery";
+
+beforeAll(() => {
+ injectGlobals();
+ vi.mock("./services/ws.js", mockWebsocket);
+ vi.mock("./services/server.js", mockServer);
+});
+
+function injectGlobals() {
+ const uncheckedWindow = window as any;
+ uncheckedWindow.$ = $;
+ uncheckedWindow.WebSocket = () => {};
+ uncheckedWindow.glob = {
+ isMainWindow: true
+ };
+}
+
+function mockWebsocket() {
+ return {
+ default: {
+ subscribeToMessages(callback: (message: unknown) => void) {
+ // Do nothing.
+ }
+ }
+ }
+}
+
+function mockServer() {
+ return {
+ default: {
+ async get(url: string) {
+ if (url === "options") {
+ return {};
+ }
+
+ if (url === "keyboard-actions") {
+ return [];
+ }
+
+ if (url === "tree") {
+ return {
+ branches: [],
+ notes: [],
+ attributes: []
+ }
+ }
+ }
+ }
+ };
+}
diff --git a/src/public/app/vitest.config.ts b/src/public/app/vitest.config.ts
index 5c9eb94f65..4d15df6cae 100644
--- a/src/public/app/vitest.config.ts
+++ b/src/public/app/vitest.config.ts
@@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
+ setupFiles: ["./test-setup.ts"],
environment: "happy-dom",
coverage: {
reporter: [ "text", "html" ]
diff --git a/src/public/app/widgets/view_widgets/calendar_view.spec.ts b/src/public/app/widgets/view_widgets/calendar_view.spec.ts
index 1a90d71a75..cb093f06cb 100644
--- a/src/public/app/widgets/view_widgets/calendar_view.spec.ts
+++ b/src/public/app/widgets/view_widgets/calendar_view.spec.ts
@@ -1,7 +1,4 @@
-import $ from "jquery";
-(window as any).$ = $;
-
-import { beforeAll, describe, it, vi } from "vitest";
+import { describe, it } from "vitest";
import utils from "../../services/utils.js";
interface NoteDefinition {
@@ -54,48 +51,6 @@ async function buildNotes(notes: NoteDefinition[]) {
}
describe("Building events", () => {
-
- beforeAll(async () => {
- (window as any).WebSocket = () => {};
- (window as any).glob = {
- isMainWindow: true
- };
-
- vi.mock("../../services/ws.js", () => {
- return {
- default: {
- subscribeToMessages(callback: (message: unknown) => void) {
- // Do nothing.
- }
- }
- }
- });
-
- vi.mock("../../services/server.js", () => {
- return {
- default: {
- async get(url: string) {
- if (url === "options") {
- return {};
- }
-
- if (url === "keyboard-actions") {
- return [];
- }
-
- if (url === "tree") {
- return {
- branches: [],
- notes: [],
- attributes: []
- }
- }
- }
- }
- };
- });
- });
-
it("supports start date", async () => {
const noteIds = await buildNotes([
{ title: "A", "#startDate": "2025-05-05" }