Open ivan-lednev opened 1 year ago
Does it work with Vite? We just call their resolver, there is no additional logic for this.
I'm also facing a similar issue to this — in my case I'm mocking a module that isn't available in a testing environment, and vitest is complaining that it can't resolve it even when it's mocked. I'm pretty fresh to vitest but coming from jest I could mock just about anything regardless of what module resolution is happening or not in the background, is that not the case with vitest?
I faced same problem when testing my obsidian plugin with vitest. After a bit of experimenting I was able to find a workaround. I created a file containing mocked parts of obsidian API that I needed (in my case it was just moment library) and configured alias in vitest config to that file
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
alias: {
obsidian: './__mocks__/obsidian.ts'
}
},
})
With this vitest stops reading obsidian package (that actually just types) and there is no longer resolve error.
Does it work with Vite? We just call their resolver, there is no additional logic for this.
@sheremet-va Not sure about OP's case, but for vite
itself I don't run into this issue so long as I include the following in my vite.config.ts
:
export default defineConfig(() => ({
// ...
build: {
// ...
rollupOptions: {
external: ['obsidian'],
},
},
});
But this does not seem to fix the issue for vitest
.
I faced same problem when testing my obsidian plugin with vitest. After a bit of experimenting I was able to find a workaround. I created a file containing mocked parts of obsidian API that I needed (in my case it was just moment library) and configured alias in vitest config to that file
import { defineConfig } from 'vitest/config' export default defineConfig({ test: { alias: { obsidian: './__mocks__/obsidian.ts' } }, })
With this vitest stops reading obsidian package (that actually just types) and there is no longer resolve error.
This worked for me, but since the alias is relative to the file importing it, I had to specify it like:
// vitest.config.ts
import path from 'path';
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
alias: {
obsidian: path.resolve(__dirname, "__mocks__/obsidian.ts"),
}
},
})
Describe the bug
I'm developing a plugin for an Electron app. The app provides type definitions to plugin devs in a library with the following
package.json
: https://github.com/obsidianmd/obsidian-api/blob/master/package.jsonSo in my code I import stuff like this:
I bundle the plugin, and the application consumes that bundle, injecting "obsidian" as an external dependency (which is not present at plugin build-time).
Once I try to mock "obsidian" either with a factory method or with
__mocks__
, Vitest gives me grief:And indeed, once I manually add a dummy
main.js
and updatepackage.json
in the library with"main": "./main.js"
, Vitest is happy.Reproduction
npm i obsidian
vi.mock("obsidian", () => { return { parseYaml() { return "result" } }; });
test("mocked method works", () => { expect(parseYaml("")).toEqual("result"); });
Used Package Manager
npm
Validations