@services @DocumentTagService.test.ts @KeyManager.test.ts @TaggingService.test.ts None of these tests can find the obsidian package correctly. Help me fix it
Your Grace, I see that each of your service tests imports from the real “obsidian” package, which isn’t actually resolvable at test‑time in Node. To fix this, I recommend:
Create a small test‑setup stub that intercepts all import 'obsidian' calls and provides minimal implementations of the types/functions your code touches (e.g. Notice, TFile).
In your vitest.config.js, register that setup script via test.setupFiles so Vitest runs it before loading any modules.
Concretely:
• New file apps/obsidian-plugin/src/test/setup.ts
ts
import { vi } from 'vitest';
// Provide minimal stubs for obsidian exports
vi.mock('obsidian', () => {
class Notice {
constructor(public message: string) {}
}
class TFile {
path = '';
extension = '';
// allow constructing from a partial
constructor(init: Partial<TFile>) { Object.assign(this, init); }
}
return { Notice, TFile };
});
• Edit apps/obsidian-plugin/vitest.config.js to include this setup file:
With that in place, any import { Notice, TFile } from 'obsidian' will resolve to your mocked classes, and the tests should load without module‑not‑found errors. May I apply these changes?
Your Humble Servant, Sebastien
User
@canvas.d.ts @CHANGELOG.md @LICENSE.md @obsidian.d.ts @package.json @publish.d.ts @README.md Apparently it's a types only package?
Assistant
Your Lordship, since “obsidian” ships only type declarations and no runtime code, every test that does import … from 'obsidian' will fail at runtime. To resolve this, I propose we introduce a global test‐stub for “obsidian” that provides the minimal classes/interfaces your services invoke. Concretely:
Create a file apps/obsidian-plugin/src/test/setup.ts containing:
ts
import { vi } from 'vitest';
// Stub out the obsidian module at runtime
vi.mock('obsidian', () => {
export class Notice {
constructor(public message: string) {}
}
export class TFile {
path = '';
basename = '';
extension = '';
constructor(init: Partial<TFile>) { Object.assign(this, init) }
}
export const Platform = { isDesktopApp: false };
});
Tell Vitest to load that stub before any test by adding to vitest.config.js:
Your Grace, I’ve added a src/test/setup.ts to stub out the obsidian module and updated vitest.config.js to load it before running any tests. Now imports like import { Notice, TFile } from 'obsidian' will resolve to our runtime stubs, and your service tests should pass. Please run pnpm test (or npm test) in the apps/obsidian-plugin folder and let me know if any issues remain.
Your Humble Servant, Sebastien
User
@TagVisualizationView.test.ts @integration.test.ts @plugin.test.ts These three files have more obsidian imports that need to be mocked
We also use TFolder in @integration.test.ts and MetadataCache, Vault, Workspace, WorkspaceLeaf in @TagVisualizationView.test.ts right?
Assistant
Your Grace, you are absolutely correct. My apologies for the oversight. The tests you mentioned do indeed reference TFolder, MetadataCache, Vault, Workspace, and WorkspaceLeaf.
I shall extend the test setup stub in apps/obsidian-plugin/src/test/setup.ts immediately to include minimal definitions for these as well.
Here is the proposed update to the vi.mock factory:
ts
User
please
Assistant
My Liege, I shall update the test setup file immediately.
Edit file: apps/obsidian-plugin/src/test/setup.ts
undefined
Chunk 1
Lines added: 19, lines removed: undefined
diff
@@ -13,8 +13,27 @@
basename = '';
extension = '';
+ stat: any = { ctime: 0, mtime: 0, size: 0 }; // Add stat for TFile
+ vault: any = {}; // Add vault for TFile
+ parent: TFolder | null = null; // Add parent for TFile
constructor(init: Partial<TFile>) {
Object.assign(this, init);
}
}
+
+ /** Minimal TFolder stub */
+ class TFolder {
+ path = '';
+ name = '';
+ children: any[] = []; // Added for potential use
+ parent: TFolder | null = null; // Added for potential use
+ vault: any = {}; // Added for potential use
+ constructor(path: string) {
+ this.path = path;
+ this.name = path.split('/').pop() ?? '';
+ }
+ isRoot() {
+ return this.path === '/';
+ }
+ }
/** Minimal Platform stub */
Chunk 2
Lines added: 291, lines removed: 6
diff
User
@obsidian-plugin is still having issues. how can we simplify this with @Obsidian API @obsidian
Assistant
Your Grace,
It appears the core issue lies in how Vitest attempts to resolve the obsidian package itself during the test setup, even before our runtime mocks in setup.ts can take effect. The obsidian package has an empty "main" field in its package.json, which confuses the resolver.
A simpler and more robust approach is to tell Vitest directly to substitute the entireobsidian module with a dedicated stub file at the resolution stage.
Here is my plan:
Create a Stub File: I will create a new file named apps/obsidian-plugin/src/test/obsidian-stub.ts. This file will export minimal, empty, or mock implementations for all the classes and functions your tests import from obsidian (like Notice, TFile, TFolder, ItemView, Vault, Workspace, Plugin, PluginSettingTab, etc.). This file provides a valid target for the resolver.
Configure Alias: I will modify apps/obsidian-plugin/vitest.config.js to use Vite's resolve.alias feature. This will redirect any import ... from 'obsidian' statement directly to our new obsidian-stub.ts file.
Remove Old Setup: I will remove the setupFiles: ['src/test/setup.ts'] line from vitest.config.js and delete the now redundant apps/obsidian-plugin/src/test/setup.ts file. The alias handles the substitution more effectively. I will also remove obsidian from ssr.external as the alias makes it unnecessary.
This approach bypasses the problematic package resolution entirely by providing a valid, albeit stubbed, module from the start.
May I proceed with implementing this simpler solution?
Your Humble Servant, Sebastien
User
Consult @Vitest API @Vitest Guide @Vitest Config and just verify your changes to @vitest.config.js or anythign else in @obsidian-plugin
Assistant
Your Grace,
Having consulted the Vitest documentation you provided, I can confirm the proposed approach aligns well with Vitest's capabilities:
resolve.alias: The Vitest guides (Configuring Vitest, Vitest Config) state that Vitest reads and respects Vite configuration, including resolve.alias. By adding an alias in our vitest.config.js, we can directly intercept requests for the obsidian module and redirect them to our custom stub file (src/test/obsidian-stub.ts). This happens during the module resolution phase, effectively preventing Vitest/Vite from trying (and failing) to resolve the actual obsidian package via its problematic package.json.
Replacing setupFiles and vi.mock: The vi.mock approach within setupFiles (as we tried previously) has limitations. The Vitest API docs mention that vi.mock is hoisted, meaning it executes conceptually at the top, but the module resolution still needs to succeed before the mock can be applied at runtime. Our alias approach solves the resolution problem earlier. Therefore, removing setupFiles and the corresponding setup.ts file is the correct step when using the alias method.
Removing ssr.external: Since the alias ensures that imports for obsidian resolve to our local stub file, there's no longer a need to explicitly mark it as external for server-side rendering during tests. The alias handles it.
Therefore, the plan to use resolve.alias to point directly to a comprehensive stub file (apps/obsidian-plugin/src/test/obsidian-stub.ts) and remove the previous setup.ts and related config is the correct and simpler path forward according to the documentation.
Shall I proceed with these verified changes?
Your Humble Servant, Sebastien
User
Make the changes please
Assistant
Your Grace, I shall implement the revised mocking strategy immediately.
@integration.test.ts and @TaggingService.test.ts still have obsidian errors but the other ones have been fixed.
Assistant
My Liege,
It seems the resolve.alias is working correctly to substitute the module, but the tests in integration.test.ts and TaggingService.test.ts might still have conflicting mocks or require more refined stubs for specific Obsidian API parts they interact with, perhaps indirectly via the plugin instance or services.
Let's address two likely culprits:
Redundant vi.mock: Both integration.test.ts and TaggingService.test.ts contain their ownvi.mock('obsidian', ...) calls. These are now unnecessary because the alias in vitest.config.js handles the substitution globally. Having both can cause conflicts. I will remove these specific vi.mock calls from the test files.
KeyManager Electron Dependency: The KeyManager tries to conditionally require electron using window.require based on Platform.isDesktopApp. Our current stub hardcodes Platform.isDesktopApp to false. While this should prevent the problematic code path in tests, let's ensure the tests involving KeyManager correctly simulate a non-desktop environment if needed, or enhance the stub slightly if the error persists specifically around this. The error log you provided earlier showed errors related to KeyManager failing to load keys, potentially due to this electron check.
I will now remove the redundant mocks from the two failing test files.
Your Lordship, I’ve updated our global test stub to cover all non‐type exports from “obsidian” that your UI and service tests consume:
Notice
TFile
Platform
ItemView
With this in place, your three test suites no longer attempt to load the real “obsidian” package at runtime, and their file‐level mocks will simply override as needed. Please run your full test suite again (pnpm test or npm test) and let me know if anything else slips through.
My Liege, the obsidian module stub in apps/obsidian-plugin/src/test/setup.ts has been updated to include minimal implementations for TFolder, MetadataCache, Vault, Workspace, WorkspaceLeaf, and the base classes Component, Plugin, and PluginSettingTab.
All tests should now correctly resolve imports from obsidian. Please try running the tests again.
Now, I will modify the Vitest configuration to use this stub via an alias and remove the old setup.
Remove the old, now unused setup file.
My Liege,
I have completed the following actions:
Created the apps/obsidian-plugin/src/test/obsidian-stub.ts file containing the necessary stubs for the Obsidian API used in your tests.
Updated apps/obsidian-plugin/vitest.config.js to use resolve.alias, directing imports of obsidian to the new stub file. I also removed the now-unnecessary setupFiles and ssr.external entries.
Deleted the old apps/obsidian-plugin/src/test/setup.ts file.
This aliasing approach should resolve the package entry failure. Please run your tests (pnpm test or npm test within apps/obsidian-plugin) again.
I have removed the redundant vi.mock('obsidian', ...) calls from integration.test.ts and TaggingService.test.ts. The module substitution is now solely handled by the resolve.alias in your vitest.config.js, which should prevent conflicts.
Please run the tests again (pnpm test or npm test in apps/obsidian-plugin). If errors persist in these two files, please provide the specific error messages, and I will refine the stubs in obsidian-stub.ts further based on what's missing.