obsidian-community / jest-environment-obsidian

A Jest environment to facilitate unit testing for Obsidian plugins.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Other]: Cannot find module '../node_modules/jest-message-util/build/index.js'

re4mat opened this issue · comments

commented

Affected Version

0.0.1

Info

The issue appears to be due to the way that the path to node_modules/jest-message-util/build/index.js is constructed in src/warning.ts.

Description

After successfully implementing workaround for #2, when attempting to run a test via npm run test, I received the following error:

 FAIL  test/settings.test.ts
  ● Test suite failed to run

    Cannot find module '../node_modules/jest-message-util/build/index.js'
    Require stack:
    - %project_root%\node_modules\jest-environment-obsidian\dist\jest-environment.js
    - %project_root%\node_modules\jest-util\build\requireOrImportModule.js
    - %project_root%\node_modules\jest-util\build\index.js
    - %project_root%\node_modules\jest-config\build\getCacheDirectory.js
    - %project_root%\node_modules\jest-config\build\Defaults.js
    - %project_root%\node_modules\jest-config\build\normalize.js
    - %project_root%\node_modules\jest-config\build\index.js
    - %project_root%\node_modules\jest-cli\build\init\index.js
    - %project_root%\node_modules\jest-cli\build\run.js
    - %project_root%\node_modules\jest-cli\build\index.js
    - %project_root%\node_modules\jest-cli\bin\jest.js
    - %project_root%\node_modules\jest\bin\jest.js

      at Object.<anonymous> (node_modules/jest-environment-obsidian/dist/jest-environment.js:132:32)
      at Object.newLoader (node_modules/pirates/lib/index.js:121:7)

(path to project root truncated for readability)

Environment

OS: Windows 10
Node: 18.16.1
Jest: 29.6.1

Steps to Reproduce

  1. Install jest-environment-obsidian via npm install --save-dev jest-environment-obsidian
  2. Change preset in jest.config.cjs to 'jest-environment-obsidian'
  3. Implement jest-preset.js workaround from issue #2
  4. Run test via npm run test

Workaround

I was able to clear this error by changing line 132 in dist/jest-environment.js to the following:

var import_jest_message_util = require("../../../node_modules/jest-message-util/build/index.js");

This causes it to go up two more directory levels before attempting to traverse node_modules/, which allows it to successfully find jest-message-util/build/index.js.

For anyone looking for an automated workaround until this is fixed, you can fix this with sed.

First, add a script in your repo called fix-jest-environment-obsidian:

#!/bin/sh
# workaround for https://github.com/obsidian-community/jest-environment-obsidian/issues/3
sed -i 's/require("..\/node_modules\/jest-message-util\//require("..\/..\/..\/node_modules\/jest-message-util\//' ./node_modules/jest-environment-obsidian/dist/jest-environment.js

Then in package.json:

{
  ...
  "scripts": {
    ...
    "test": "sh fix-jest-environment-obsidian && jest --coverage",
    "test:watch": "sh fix-jest-environment-obsidian && jest --coverage --watch"
    ...
  }
}