Node.js Jest测试不';t创建正常程序会创建的文件
我使用node和puppeter加载一个页面,获取其内容,然后创建一个屏幕截图(如果有)。在run函数的末尾,我有以下几行Node.js Jest测试不';t创建正常程序会创建的文件,node.js,jestjs,puppeteer,jest-puppeteer,Node.js,Jestjs,Puppeteer,Jest Puppeteer,我使用node和puppeter加载一个页面,获取其内容,然后创建一个屏幕截图(如果有)。在run函数的末尾,我有以下几行 var content = fs.writeFileSync(outputFilePath, processedContent); var screenshot = page.screenshot({path: '../output/whatever.png', fullPage:true}) browser.close(); 这在运行节点应用程序时有效。为了测试,我使用
var content = fs.writeFileSync(outputFilePath, processedContent);
var screenshot = page.screenshot({path: '../output/whatever.png', fullPage:true})
browser.close();
这在运行节点应用程序时有效。为了测试,我使用了玩笑
在尝试运行JEST测试以检查屏幕截图时:
it('Run should take a screenshot', async() => {
const runResult = await run();
const screenshot = fs.readFileSync('/app/output/whatever.png');
expect(screenshot).toBeTruthy();
})
我得到了以下错误:没有这样的文件或目录,打开“/app/output/whatever.png”
我很难理解为什么在正常的应用程序流程中,程序运行时会创建文件,但在测试中却不会。作为附加信息,整个内容在Docker容器中运行,这很可能是因为您在jest测试中使用的是绝对路径而不是相对路径 所以不是
const screenshot = fs.readFileSync('/app/output/whatever.png');
写
使用相对路径
还要记住,您的相对路径应该是从项目根目录开始的。我可以做什么
。
要升级一级:('../app/output/whatever.png')
,它不起作用。
const screenshot = fs.readFileSync('./app/output/whatever.png');