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.
Installation
Section titled “Installation”npm install --save-dev obsidian-test-mocksPeer dependencies: obsidian, plus @codemirror/state, @codemirror/view and moment, which obsidian
already brings into the tree. They are peers so the mocks share your project’s one CodeMirror copy rather
than nesting a second one, whose facets, state fields and instanceof checks would not match yours.
Entry points
Section titled “Entry points”| 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 |
Your first test
Section titled “Your first test”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.
The __ suffix
Section titled “The __ suffix”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.
Where to next
Section titled “Where to next”- Test Runner Setup — Vitest, Jest, and everything else.
- Importing the Mocks — why test files import from
obsidian-test-mocks/obsidianrather thanobsidian. - Creating Mock Instances — factories, spying on
construction, and the pre-configured
App. - Strict Mocks — what happens when you touch something that is not mocked, and how to fill the gap.
- Type Bridging — moving between mock types and real obsidian types.
- Using with
obsidian-typings— bridging the internal API surface. - Network Requests — what
requestUrlanswers, and how to give a test its own responses. - API reference — the complete, searchable API generated from the library’s TSDoc.