开玩笑的测试不会创建正常程序会创建的文件

时间:2019-07-19 07:55:59

标签: node.js jestjs puppeteer jest-puppeteer

我正在使用node和puppeteer加载页面,获取页面内容,然后创建一个屏幕截图(如果有)。在运行功能的最后,我有以下几行

var content = fs.writeFileSync(outputFilePath, processedContent);
var screenshot = page.screenshot({path: '../output/whatever.png', fullPage:true})
browser.close();

这在运行节点应用程序时有效。为了测试,我正在使用JEST 并尝试运行用于检查屏幕截图的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容器中运行

1 个答案:

答案 0 :(得分:0)

最可能是因为在笑话测试中使用的是绝对路径,而不是相对路径。

所以不是

const screenshot = fs.readFileSync('/app/output/whatever.png');

const screenshot = fs.readFileSync('./app/output/whatever.png');

使用相对路径

还请记住,您的相对路径应从项目根目录开始

相关问题