shelfio / jest-mongodb

Jest preset for MongoDB in-memory server
MIT License
591 stars 83 forks source link

Error in 4.1.3 - Cannot read properties of undefined (reading 'rootDir') #395

Closed ArthurHoaro closed 1 year ago

ArthurHoaro commented 1 year ago

Hi, when upgrading to the latest version (4.1.3), I get this error when trying to run jest:

 ➜ ./node_modules/.bin/jest                               
TypeError: Jest: Got error running globalSetup - /home/arthur/Downloads/tmp/jest-setup.js, reason: Cannot read properties of undefined (reading 'rootDir')
    at module.exports (/home/arthur/Downloads/tmp/node_modules/@shelf/jest-mongodb/lib/setup.js:17:51)
    at module.exports (/home/arthur/Downloads/tmp/jest-setup.js:3:9)
    at /home/arthur/Downloads/tmp/node_modules/@jest/core/build/runGlobalHook.js:109:19
    at ScriptTransformer.requireAndTranspileModule (/home/arthur/Downloads/tmp/node_modules/@jest/transform/build/ScriptTransformer.js:780:24)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runGlobalHook (/home/arthur/Downloads/tmp/node_modules/@jest/core/build/runGlobalHook.js:101:9)
    at async runJest (/home/arthur/Downloads/tmp/node_modules/@jest/core/build/runJest.js:317:5)
    at async _run10000 (/home/arthur/Downloads/tmp/node_modules/@jest/core/build/cli/index.js:326:7)
    at async runCLI (/home/arthur/Downloads/tmp/node_modules/@jest/core/build/cli/index.js:191:3)
    at async Object.run (/home/arthur/Downloads/tmp/node_modules/jest-cli/build/run.js:124:37)

I suppose this was introduced by changes in #389 (line). projectConfig is undefined for some reason. Maybe it's because I run setup in a globalconfig script?

Here is a minimal project to reproduce the issue: https://stackblitz.com/edit/node-yqpcqz

ajwootto commented 1 year ago

Hey @ArthurHoaro apologies for the trouble from that change.

I think in this case you should be able to just forward the config that should be getting passed to your setup function into the library's setup function like so:

// jest-setup.js
const mongoSetup = require('@shelf/jest-mongodb/lib/setup');

module.exports = async (config) => {
  await mongoSetup(config);
};

Does that resolve the issue?

I didn't account for the case of the setup method being wrapped this way. Normally if the preset is just plugged straight into Jest it will always receive that configuration object. Is there something the library should do here to account for this case?

ArthurHoaro commented 1 year ago

Thanks for your quick response @ajwootto. It's working as expected when passing the configuration to the setup function, as in your example.

I don't think it's necessary to update anything in the lib following this change, I'm glad that it can be solved easily in the setup call.