This document explains how to run tests for the obsidian-tracker plugin.
npm testThat's it! The tests will run automatically.
npm test # Run all tests once
npm run test:watch # Run tests in watch mode (re-runs on file changes)
npm run test:coverage # Run tests with coverage reportUnit tests validate core logic without requiring Obsidian to be running. They test:
- Data collection functions - How data is extracted from frontmatter, tags, etc.
- Parsing functions - YAML configuration parsing and validation
- Edge cases - Empty values, null, undefined, arrays, booleans, etc.
test/frontmatter-exists.test.ts- Tests for thefrontmatter.existssearchType- 12 test cases covering all edge cases
- Validates non-empty strings, arrays, booleans, numbers
- Validates empty strings, arrays, null, undefined are rejected
test/
├── frontmatter-exists.test.ts # Unit tests for frontmatter.exists
├── mocks/
│ ├── obsidian.ts # Mock Obsidian API
│ └── d3.ts # Mock d3 library
└── setup.ts # Jest setup file
- Node.js - Version 18+ recommended
- npm - Comes with Node.js
- Dependencies - Run
npm installfirst
# Install dependencies (including Jest)
npm install
# Verify tests work
npm testIf you encounter issues with npm install (e.g., obsidian package integrity errors), see Troubleshooting below.
import { describe, it, expect, beforeEach } from '@jest/globals';
import { SearchType, Query } from '../src/data';
import { yourFunction } from '../src/your-module';
describe('Your Feature', () => {
let query: Query;
beforeEach(() => {
// Set up test data
query = new Query(0, SearchType.YourType, 'target');
});
it('should do something', () => {
const result = yourFunction(query);
expect(result).toBe(true);
});
});- Test files should be named
*.test.tsor*.spec.ts - Place them in the
test/directory - Jest will automatically find and run them
Since tests run outside Obsidian, we mock the Obsidian API. See test/mocks/obsidian.ts for examples.
To use mocks:
import type { CachedMetadata } from 'obsidian';
const fileCache: CachedMetadata = {
frontmatter: {
field: 'value'
}
};If you see integrity check errors for the obsidian package:
# Clear npm cache
npm cache clean --force
# Remove node_modules and reinstall
rm -rf node_modules package-lock.json
npm installMake sure all dependencies are installed:
npm installCheck that ts-jest is installed:
npm install --save-dev ts-jest@29 @types/jest@29These tests can be run in CI/CD pipelines:
# Example GitHub Actions
- name: Run tests
run: npm testFor visual/end-to-end testing, you still need to test in Obsidian:
- Build the plugin:
npm run build - Copy to test vault: See
test-deploy.shorTEST_SETUP.md - Enable plugin in Obsidian
- Test with real notes
See docs/dev/issue-497/TESTING_GUIDE.md for detailed manual testing instructions.
Generate coverage reports:
npm run test:coverageThis creates a coverage/ directory with HTML reports. Open coverage/lcov-report/index.html in a browser to view.
- Write tests for new features - Add tests when implementing new searchTypes or features
- Test edge cases - Empty values, null, undefined, arrays, etc.
- Keep tests fast - Unit tests should run in seconds
- Mock external dependencies - Don't require Obsidian or real files
- Use descriptive test names - "should count non-empty strings" not "test1"
- See
TEST_SETUP.mdfor setup troubleshooting - Check existing test files for examples
- Review Jest documentation: https://jestjs.io/docs/getting-started