'GraphQL Tools loadFilesSync works for runtime but not jest in NodeJS - why?

I am working on setting up an Apollo GraphQL Server (Express based) and use the pattern where .graphql files are loaded and merged as demonstrated in this guide. This leads me to use loadFilesSync, path and the Node provided __dirName.

My typedefs module looks like this

const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');

const schemaDefsArray = loadFilesSync(
    path.join(__dirname, './types'),
    {
        recursive: true,
        extensions: ['graphql'],
    })

const typeDefs = mergeTypeDefs(schemaDefsArray);

module.exports = typeDefs;

My setup is a docker container built from a repo checked out to the WSL file system in Windows. Both the Apollo/Express server and Jest tests are run on Node from inside that container.

My problem is that during regular runtime (Express based Apollo GraphQL Server), __dirName is /srv/<project name>/src/schema and all .graphql-files are resolved for the schema as intended. But during test-runs (Jest running directly on Node) __dirName is a fully qualified \\wsl$\Ubuntu-20.04\home\<user name>\projects\<project name>\src\schema. The Apollo Server loads, reads, and resolves the schema fine while Jest tests attempting to load the same files through the same module cannot find the directory.

Why is the working directory resolved differently, what does it mean for the loadFiles function and how do I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source