vitest-dev / vitest-dev/vitest

Fails to mock module from lib if no `"main"` entry in `package.json`

Open
#4,029 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

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

So in my code I import stuff like this:

import { parseYaml } from "obsidian";

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:

Error: Failed to resolve entry for package "obsidian". The package may have incorrect main/module/exports specified in its package.json.
 ❯ packageEntryFailure node_modules/vite/dist/node/chunks/dep-df561101.js:28691:11
 ❯ resolvePackageEntry node_modules/vite/dist/node/chunks/dep-df561101.js:28688:5
 ❯ tryNodeResolve node_modules/vite/dist/node/chunks/dep-df561101.js:28419:20
 ❯ Context.resolveId node_modules/vite/dist/node/chunks/dep-df561101.js:28180:28
 ❯ Object.resolveId node_modules/vite/dist/node/chunks/dep-df561101.js:44207:32
 ❯ MessagePort.<anonymous> node_modules/vitest/dist/vendor-index.b271ebe4.js:59:20

And indeed, once I manually add a dummy main.js and update package.json in the library with "main": "./main.js", Vitest is happy.

Reproduction
  1. Install the library: npm i obsidian
  2. Create a test file
  3. Add and run the following test:
import { parseYaml } from "obsidian";

vi.mock("obsidian", () => {
  return {
    parseYaml() {
      return "result"
    }
  };
});

test("mocked method works", () => {
  expect(parseYaml("")).toEqual("result");
});

System Info
System:
    OS: Windows 10 10.0.22621
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 9.56 GB / 31.50 GB
  Binaries:
    Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.5.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.2134.0), Chromium (116.0.1938.54)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    vite: ^4.4.9 => 4.4.9
    vitest: ^0.34.3 => 0.34.3
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction: install obsidian, run the TypeScript test containing vi.mock("obsidian"), and inspect how the package.json without a main entry is resolved. The fix is done when the mocked module works without adding a dummy main.js, while the existing reproduction still passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vite
Domain
build-system, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.