Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
re4mat opened this issue Jul 26, 2023 · 1 comment

Comments

@re4mat
Copy link

re4mat commented Jul 26, 2023

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 [Other]: Module jest-environment-obsidian should have "jest-preset.js" or "jest-preset.json" file at the root. #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.

@chrisguidry
Copy link

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"
    ...
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants