refactor(test): global initialisation

This commit is contained in:
Elian Doran 2025-02-28 19:11:12 +02:00
parent 7a34a2f59c
commit f646e0f724
No known key found for this signature in database
3 changed files with 53 additions and 46 deletions

View File

@ -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: []
}
}
}
}
};
}

View File

@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";
export default defineConfig({ export default defineConfig({
test: { test: {
setupFiles: ["./test-setup.ts"],
environment: "happy-dom", environment: "happy-dom",
coverage: { coverage: {
reporter: [ "text", "html" ] reporter: [ "text", "html" ]

View File

@ -1,7 +1,4 @@
import $ from "jquery"; import { describe, it } from "vitest";
(window as any).$ = $;
import { beforeAll, describe, it, vi } from "vitest";
import utils from "../../services/utils.js"; import utils from "../../services/utils.js";
interface NoteDefinition { interface NoteDefinition {
@ -54,48 +51,6 @@ async function buildNotes(notes: NoteDefinition[]) {
} }
describe("Building events", () => { 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 () => { it("supports start date", async () => {
const noteIds = await buildNotes([ const noteIds = await buildNotes([
{ title: "A", "#startDate": "2025-05-05" } { title: "A", "#startDate": "2025-05-05" }