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 pathDescription
obsidian-test-mocks/obsidianMocks for every class/function in obsidian.d.ts
obsidian-test-mocks/setupExports setup() / teardown() for prototype extensions and globals
obsidian-test-mocks/vitest-setupOne-stop Vitest setup file: calls setup() + mocks the obsidian module
obsidian-test-mocks/jest-setupJest setup file: calls setup() for prototype extensions and globals
obsidian-test-mocks/obsidian-typings/setupExports setup() / teardown() for obsidian-typings bridges
obsidian-test-mocks/obsidian-typings/vitest-setupVitest setup file: auto-calls obsidian-typings bridge setup()
obsidian-test-mocks/obsidian-typings/jest-setupJest setup file: auto-calls obsidian-typings bridge setup()

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.