最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest "Could not locate module" if any dependency has "src" in path - Stack Over

programmeradmin1浏览0评论

In my react app, I am importing a module as follows:

import { isValidAddress } from 'orbit-db';

Some dependency of isValidAddress requires multicodec/src/base-table (see this line).

However, jest gives the following error:

Configuration error:

Could not locate module multicodec/src/base-table mapped as:
/Users/h/Documents/code/orbit-db-time-machine/src/base-table.

Please check your configuration for these entries:
{
  "moduleNameMapper": {
    "/src\/(.*)$/": "/Users/h/Documents/code/orbit-db-time-machine/src/$1"
  },
  "resolver": null
}

I think that this is happening because the jest dependency rule "/src\/(.*)$/" is re-writing all paths with src in them to <rootDir>/src, even if they e from a npm module.

I tried to fix this by adding the following config to my package.json:

"jest": {
  "moduleNameMapper": {
    "/multicodec\/(.*)$/": "<rootDir>/node_modules/multicodec/$1"
  }
}

However, this did not change the error. The only way I have been able to remove the error is by removing my import statement - however, this is required in my code.

Note: The code works when run from my react app, it only breaks when I run jest.

In my react app, I am importing a module as follows:

import { isValidAddress } from 'orbit-db';

Some dependency of isValidAddress requires multicodec/src/base-table (see this line).

However, jest gives the following error:

Configuration error:

Could not locate module multicodec/src/base-table mapped as:
/Users/h/Documents/code/orbit-db-time-machine/src/base-table.

Please check your configuration for these entries:
{
  "moduleNameMapper": {
    "/src\/(.*)$/": "/Users/h/Documents/code/orbit-db-time-machine/src/$1"
  },
  "resolver": null
}

I think that this is happening because the jest dependency rule "/src\/(.*)$/" is re-writing all paths with src in them to <rootDir>/src, even if they e from a npm module.

I tried to fix this by adding the following config to my package.json:

"jest": {
  "moduleNameMapper": {
    "/multicodec\/(.*)$/": "<rootDir>/node_modules/multicodec/$1"
  }
}

However, this did not change the error. The only way I have been able to remove the error is by removing my import statement - however, this is required in my code.

Note: The code works when run from my react app, it only breaks when I run jest.

Share Improve this question edited Oct 13, 2019 at 7:04 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Oct 12, 2019 at 23:52 Hannes HertachHannes Hertach 5295 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

This was a bug and has been fixed: https://github./facebook/create-react-app/issues/7818

I remember ing across a similar problem when using the KY library in my React project with Jest. You might have to also use transformIgnorePatters to overe this.

There was an issues thread where they discussed this in the KY repo that you might also find helpful, although some of the solutions are very specific to that project: https://github./sindresorhus/ky/issues/170

发布评论

评论列表(0)

  1. 暂无评论