graphile-contrib/postgraphile-plugin-connection-filter

Do not use dynamic require in main function

jrmdayn opened this issue · 0 comments

I am trying to run e2e tests via jest. I spin up an express app that run my postgraphile middleware that eventually loads your plugin. My test looks pretty much like this:

beforeAll( async () => {
    const app = express();
    app.use(postgraphileApp);
    server = await app.listen();
    console.log('app started'):
});

it(....)

afterAll(async () => {
    await server.close();
    console.log('app closes'):
});

When I run this, I can see that the app starts and stops on the console but I get a jest error at the end:

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at PostGraphileConnectionFilterPlugin (node_modules/postgraphile-plugin-connection-filter/index.js:26:3)
      at Object.<anonymous>.exports.getBuilder (node_modules/graphile-build/node8plus/index.js:83:11)
A fatal error occurred when building the initial schema, so the process will now exit. Error details:

TypeError: require(...) is not a function
    at PostGraphileConnectionFilterPlugin (node_modules/postgraphile-plugin-connection-filter/index.js:26:48)
    at Object.plugin [as getBuilder] (node_modules/graphile-build/src/index.js:60:11)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
  ●  process.exit called with "1"

The problem comes from the dynamic require usage in index.js. I understand that it is done like this for performance reason. Would you consider not using dynamic require for better usability / testing?

I'll send a PR. You can decide 😄 !