Test Runner Setup
Two things have to happen before a test can run plugin code: the prototype extensions and globals
Obsidian installs have to be applied, and import ... from 'obsidian' has to resolve to the mocks. The
runner-specific setup entry points do both.
Vitest
Section titled “Vitest”Add the Vitest setup file — it patches prototypes/globals and mocks obsidian automatically:
import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { server: { deps: { inline: ['@obsidian-typings', 'obsidian-dev-utils'] } }, setupFiles: [ 'obsidian-test-mocks/vitest-setup', 'obsidian-test-mocks/obsidian-typings/vitest-setup' ] }});The second setup file is optional — see
Using with obsidian-typings.
Add the Jest setup file and a moduleNameMapper entry aliasing the obsidian module:
module.exports = { moduleNameMapper: { '^obsidian$': 'obsidian-test-mocks/obsidian' }, setupFiles: ['obsidian-test-mocks/jest-setup']};Other frameworks
Section titled “Other frameworks”The vitest-setup and jest-setup entry points already handle prototype/global patching and obsidian
module aliasing. With a different framework you do both yourself, using the generic setup entry point:
-
Prototype/global patching — call
setup()/teardown()in your lifecycle hooks:import {setup,teardown} from 'obsidian-test-mocks/setup';beforeAll(() => setup());afterAll(() => teardown()); -
Module aliasing — redirect
import ... from 'obsidian'to the mocks so that your production code under test receives mock implementations. For reference, here is whatvitest-setupdoes:vi.mock('obsidian', async () => await import('obsidian-test-mocks/obsidian'));Write something similar using your framework’s module mocking API, or configure module resolution at the config level (as the Jest example above does with
moduleNameMapper).