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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

can i do .. to go one level up: ('../app/output/whatever.png'), it does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.