Skip to content

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.

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']
};

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:

  1. Prototype/global patching — call setup() / teardown() in your lifecycle hooks:

    import {
    setup,
    teardown
    } from 'obsidian-test-mocks/setup';
    beforeAll(() => setup());
    afterAll(() => teardown());
  2. Module aliasing — redirect import ... from 'obsidian' to the mocks so that your production code under test receives mock implementations. For reference, here is what vitest-setup does:

    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).