Skip to content

Getting Started

obsidian-test-mocks provides in-memory implementations of every class and function in obsidian.d.ts, plus the prototype extensions Obsidian adds to DOM and JavaScript builtins. Point your test runner at it and plugin code that does import { ... } from 'obsidian' runs in a plain Node/jsdom process — no vault, no Electron, no real Obsidian.

Terminal window
npm install --save-dev obsidian-test-mocks

Peer dependency: obsidian.

Import path Description
obsidian-test-mocks/obsidian Mocks for every class/function in obsidian.d.ts
obsidian-test-mocks/setup Exports setup() / teardown() for prototype extensions and globals
obsidian-test-mocks/vitest-setup One-stop Vitest setup file: calls setup() + mocks the obsidian module
obsidian-test-mocks/jest-setup Jest setup file: calls setup() for prototype extensions and globals
obsidian-test-mocks/obsidian-typings/setup Deprecated no-op. Bridging is no longer needed
obsidian-test-mocks/obsidian-typings/vitest-setup Deprecated no-op. Bridging is no longer needed
obsidian-test-mocks/obsidian-typings/jest-setup Deprecated no-op. Bridging is no longer needed

Wire up the setup file for your runner — see Test Runner Setup — then build a vault in memory and exercise the code under test:

import { App } from 'obsidian-test-mocks/obsidian';
import {
expect,
it
} from 'vitest';
it('reads a note out of the vault', async () => {
const app = App.createConfigured__({
files: {
'notes/daily/2024-01-01.md': '# New Year'
}
});
const file = app.vault.getFileByPath('notes/daily/2024-01-01.md');
expect(file).not.toBeNull();
expect(await app.vault.read(file!)).toBe('# New Year');
});

createConfigured__() builds a fully wired App, creating the notes and notes/daily folders from the file paths automatically.

Any member ending in __ is mock-only — it does not exist in the real Obsidian API. That covers the factories (create__()), the type bridges (asOriginalType__() / fromOriginalType__()), and the test helpers (simulateClick__(), simulateChange__()). Everything without the suffix is a faithful stand-in for something obsidian.d.ts declares.