From f646e0f724583e622f55de7d5ac3b0c9834c9036 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 28 Feb 2025 19:11:12 +0200 Subject: [PATCH] refactor(test): global initialisation --- src/public/app/test-setup.ts | 51 +++++++++++++++++++ src/public/app/vitest.config.ts | 1 + .../view_widgets/calendar_view.spec.ts | 47 +---------------- 3 files changed, 53 insertions(+), 46 deletions(-) create mode 100644 src/public/app/test-setup.ts diff --git a/src/public/app/test-setup.ts b/src/public/app/test-setup.ts new file mode 100644 index 000000000..75ca3ae6e --- /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 5c9eb94f6..4d15df6ca 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 1a90d71a7..cb093f06c 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" }