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'] }});[!NOTE] The
server.deps.inlinesetting tells Vitest to bundle@obsidian-typingsandobsidian-dev-utilsinto the test transform pipeline instead of treating them as external Node.js imports. Without this, Vitest may fail to resolve these transitive dependencies at runtime. Add any other packages that causeCannot find moduleerrors during test setup to this list.
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']};[!NOTE] Unlike Vitest, Jest requires
moduleNameMapperbecause theobsidiannpm package is types-only (no JS runtime) andjest.mockin setup files cannot resolve it.
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).
[!WARNING]
If your test framework does not support module mocking or aliasing, it cannot be used with this library.
Production code under test does
import { ... } from 'obsidian', and without module aliasing those imports will not resolve to the mocks.