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

In my react app, I am importing a module as follows:import { isValidAddress } from 'orbit-db'

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281244a4414526.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信