mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-28 18:42:28 +08:00
refactor(test): global initialisation
This commit is contained in:
parent
7a34a2f59c
commit
f646e0f724
51
src/public/app/test-setup.ts
Normal file
51
src/public/app/test-setup.ts
Normal 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: []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
setupFiles: ["./test-setup.ts"],
|
||||
environment: "happy-dom",
|
||||
coverage: {
|
||||
reporter: [ "text", "html" ]
|
||||
|
@ -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" }
|
||||
|
Loading…
x
Reference in New Issue
Block a user