简体   繁体   中英

React native absolute path - Jest setup

I'm using this technique to import packages in a react native project

https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1

It suggests to

1) Add a package.json file with { “name”: “FOLDER_NAME” } in it to the folder you'd like to import from.

2) import Thing from 'FOLDER_NAME/thing'.

This works but my Jest tests are unable to find those modules.

How can I setup Jest to find those modules?

You can tell Jest to search your modules in your root folder by adding this to package.json :

"jest": {
    "moduleDirectories": ["node_modules", "/"]

Note the default value is node_modules so you should include it as well.

You can also tell it to search in your specific folder depending on your folder structure:

"jest": {
    "moduleDirectories": ["node_modules", "FOLDER_NAME"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM