Susy11
Susy11

Reputation: 260

Jest test doesn't create the files the normal program would

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

Upvotes: 0

Views: 2194

Answers (1)

Software Person
Software Person

Reputation: 2846

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

Upvotes: 3

Related Questions