diff --git a/tests/setup.ts b/tests/setup.ts new file mode 100644 index 0000000..aa1ebdc --- /dev/null +++ b/tests/setup.ts @@ -0,0 +1,24 @@ +/** + * Test setup — polyfill window for Obsidian plugin compatibility. + * Obsidian plugins use window.setTimeout/window.clearTimeout, + * but tests run in Node.js where window doesn't exist. + */ + +if (typeof globalThis.window === 'undefined') { + // Create a proxy that always delegates to globalThis + // This ensures vi.useFakeTimers() works correctly + (globalThis as any).window = new Proxy(globalThis, { + get(target, prop) { + return (target as any)[prop]; + } + }); +} + +if (typeof globalThis.activeDocument === 'undefined') { + (globalThis as any).activeDocument = { + querySelectorAll: () => [], + createElement: (tag: string) => ({ id: '', textContent: '', style: {} }), + head: { appendChild: () => {} }, + getElementById: () => null, + }; +} diff --git a/vitest.config.ts b/vitest.config.ts index 7832f4e..afb9cef 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -5,6 +5,7 @@ export default defineConfig({ test: { globals: true, environment: "node", + setupFiles: ["tests/setup.ts"], include: [ "tests/**/*.test.ts", "tests/**/*.property.test.ts",