简体   繁体   English

开玩笑未能从相对路径计算绝对文件路径

[英]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 我正在开玩笑地测试javascript文件,该文件具有相对的文件路径,例如

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 . 如果我通过开玩笑地测试上述javascript文件,通过导入到test.js文件,我得到的是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 ). 除了模拟文件路径请求或提供绝对文件路径外,还有什么方法可以测试这些文件(因为javascript代码应在任何计算机上运行。建议不要删除相对路径)。

This is not a problem with jest per se, but rather fs.readFileSync as some fs methods uses process.cwd() to compute pathing. 开玩笑本身不是问题,而是fs.readFileSync因为某些fs方法使用process.cwd()计算路径。

See docs: "The relative path to a filename can be used. Remember, however, that this path will be relative to process.cwd()." 请参阅文档: “可以使用文件名的相对路径。但是请记住,该路径将相对于process.cwd()。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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