Testing the real filesystem with Jest returns empty results

Viewed 37

I am using Jest (in combination with TypeScript in a Node.js environment) to test the real file system (not a mock) if a file content matches the expected result.

My test looks like this:

    test("getting a existing template", async () =>{
        await get_template(template_name).then((template)=>{
            console.log('template: ', template);
            expect(template).toBe(user_input_template);
        });
    });

The function get_template() is implemented as such:

export async function get_template(template_name: string) {
    let path = join(__dirname, `../templates/template-data/${template_name}.hbs`);

    return await read_template(path).then((data) => {
        console.log('common.ts: ',data);
        return data;
    });
}

and the read_template() function like so:

export async function read_template(path: string){
    return readFileSync(path).toString();
}

When manually calling the get_template() function like so:

await get_template('template_name');`

the correct output string is received. But when calling the function via the Jest testing environment only an empty string is returned.

Have I chosen a wrong approach to testing, have I made an obvious mistake testing this asynchronous function or is this a problem with Jest specifically?

0 Answers
Related