简体   繁体   中英

Jest failed to compute the absolute file path from relative path

I am Testing the javascript file with jest which having relative file path like

fs.readFileSync('../configuration_service/configuration_files/test.js') 

If I test the above javascript file with jest, by importing into the test.js file, I am getting ENOENT: no such file or directory . Is there any way to test those file, other than mocking the file path request or providing the absolute file path (Because the javascript code should run on any machine.Removing the relative path is not advisable ).

This is not a problem with jest per se, but rather fs.readFileSync as some fs methods uses process.cwd() to compute pathing.

See docs: "The relative path to a filename can be used. Remember, however, that this path will be relative to process.cwd()."

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