0

I'm using node and puppeteer to load a page, get its content and then create a screenshot if it. At the end of the run function I have the following lines

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

This works when running the node app. For testing I am using JEST And when trying to run the JEST test that checks for the screenshot:

it('Run should take a screenshot', async() => {
    const runResult = await run();
    const screenshot = fs.readFileSync('/app/output/whatever.png');
    expect(screenshot).toBeTruthy();
})

I get the following error ENOENT: no such file or directory, open '/app/output/whatever.png'

I'm having a hard time understanding why in the normal app flow the program creates the files when running but in the tests it doesn't. As additional info the entire thing runs in a Docker container

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Susy11
  • 260
  • 1
  • 3
  • 15

1 Answers1

3

It is most likely because you are using an absolute path instead of a relative path in your jest test.

So instead of

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

write

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

to use a relative path

Also keep in mind your relative path should be from the the project root

Software Person
  • 2,526
  • 13
  • 18
  • can i do `..` to go one level up: `('../app/output/whatever.png')`, it does not work. – Timo Mar 23 '21 at 19:18