Sample Plugin

unlisted

by Obsidian

Demonstrates some of the capabilities of the Obsidian API.

★ 1 starsUpdated 11d ago0BSD
View on GitHub

Obsidian Playwright Template

This is a template for UI testing in Obsidian plugins.

Features:

  • Runs Playwright tests, each on separate temporary vaults, using a separate Obsidian installation that won't interfere with any of your own vaults on the same machine.
  • Github Actions workflow to run tests in CI.
  • Hot plugin reloading on test vaults
  • Playwright code generation support

Setup

1. Set up Playwright

First, clone this repo into a new folder.

If you're developing a new plugin, you can use this repo (which is based on the Obsidian Sample Plugin) as a starting point.

To set up on an existing plugin:

  • copy the following files and folders into your plugin's project folder:
    • e2e-tests/
    • scripts/
    • playwright.config.ts
    • .github/workflows/ci.yml (if you want to run tests in CI)
  • Copy all scripts beginning with e2e from package.json
  • Install dependencies: npm i --save-dev @electron/asar@4.0.1 @playwright/test@1.58.0 concurrently@9.2.1 cross-env@10.1.0 electron@39.2.7 obsidian-typings@npm:@obsidian-typings/obsidian-public-1.13.4@1.3.0 wait-on@9.0.5
  • Update configurations:
    • tsconfig.json: Add "node", "obsidian-typings" to types, add "e2e-tests/**/*.ts", "playwright.config.ts" to include, and set "allowJs": true (the tests import a JS module from scripts/lib)
    • ESLint: add this to the flat config
      {
        // permit node imports in tests
        files: ['**/*.test.ts', '**/*.spec.ts', 'e2e-tests/**'],
        languageOptions: {
          globals: {
            ...globals.node,
          },
        },
        rules: {
          'import/no-nodejs-modules': 'off',
          // test infrastructure runs in Node, outside any vault, and reports to the terminal
          'no-console': 'off',
          'obsidianmd/hardcoded-config-path': 'off',
        },
      }
      
  • Remove sample tests (sample.spec.ts) or exclude them in Playwright config

2. Set up the test vault for your plugin

  • In e2e-tests\setup\test-vault\.obsidian\community-plugins.json, replace the sample plugin ID with your plugin's ID from manifest.json.
  • Tweak the json configuration files as desired.
  • Add any notes or other files that you wish to use in tests directly into e2e-tests\setup\test-vault\.

3. First-time Obsidian setup (per machine):

This creates a separate Obsidian installation for testing.

npm run e2e:setup-obsidian

By default, this uses the Obsidian installer from your machine. To download the latest desktop release from GitHub instead (as the CI workflow does), pass the CI flag:

  • Linux/Mac: npm run e2e:setup-obsidian -- --ci
  • Windows: node scripts/setup-obsidian.mjs -CI (call the script directly, since npm run doesn't forward arguments when run from PowerShell)

This requires the GitHub CLI (gh), plus 7-Zip on Windows. To download a specific version instead of the latest, set the OBSIDIAN_VERSION environment variable (e.g., 1.13.4).

4. Set up CI

Ensure Github Actions is enabled for your plugin's repository.

Usage

[!NOTE] Linux users may need to use Xvfb for Playwright, especially the inspector (which is used for codegen).

For example, xvfb-run --auto-servernum npm run e2e

(Optional) Verify test vaults can be created: npm run e2e:verify-setup

Run tests: npm run e2e. Results will be output to e2e-tests/test-results.

By default, tests keep Obsidian's windows off-screen so they don't pop up over your work or steal focus. Other ways to run them:

  • npm run e2e:headed: show the Obsidian windows (this is what CI uses)
  • npm run e2e:ui: use Playwright's UI mode
  • npm run e2e:keep-vaults: keep the temporary vaults in e2e-tests/test-vaults instead of deleting them, so you can inspect them
  • npm run e2e:repeat: run each test 10 times to find flaky tests

Before each run, you'll see a warning if your testing copy of Obsidian is older than the latest release, which is what CI tests against. Repeat step 3 of the setup to update it, or set OBSIDIAN_VERSION_CHECK=0 to skip the check.

Writing tests

This is largely the same as writing Playwright tests for a browser app, with some caveats:

  • Import test from e2e-tests/setup/fixtures.ts instead of @playwright/test, so that the vault copies a test creates are deleted when it ends.
  • You may need to use window.waitForTimeout() to wait for Obsidian to finish processing a command, especially when tests run in CI.
  • Obsidian context menus seem to be invisible to Playwright on Mac. (If you have a fix for this, please contact me!)
  • Playwright's codegen command (npx playwright codegen) doesn't work; use the e2e:codegen script instead.

Helper functions for some common Obsidian actions can be found in e2e-tests/helpers.ts.

See:

Hot reloading on test vaults

Run e2e:codegen. This will run ESBuild in watch mode and launch Obsidian on a fresh copy of the test vault.

[!NOTE] Changes that happen in this copy (such as editing notes or changing hotkeys and other settings) will not affect the original test vault.

To see subsequent code changes, run Reload app without saving after the build completes.

Acknowledgements

Thanks to qawatake for providing the example setup that this is based on!

For plugin developers

Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.