jest-mongodb icon indicating copy to clipboard operation
jest-mongodb copied to clipboard

`jest.config.js` does not work if `"type": "module"` is set in package.json

Open SamTolmay opened this issue 4 years ago • 4 comments

We are busy converting all our packages to ES Modules. The config in jest.config.js is not being read.

The relevant function is:

// helpers.js
module.exports.getMongodbMemoryOptions = function () {
  try {
    const {mongodbMemoryServerOptions} = require(resolve(cwd, 'jest-mongodb-config.js'));

    return mongodbMemoryServerOptions;
  } catch (e) {
    return {
      binary: {
        skipMD5: true,
      },
      autoStart: false,
      instance: {},
    };
  }
};

The require statement errors, and then the default config is returned. The error message is:

Error [ERR_REQUIRE_ESM]: require() of ES Module /PATH_TO_PACKAGE/jest-mongodb-config.js from /PATH_TO_PACKAGE/.yarn/unplugged/@shelf-jest-mongodb-virtual-5e3808f2ae/node_modules/@shelf/jest-mongodb/helpers.js not supported.
jest-mongodb-config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename jest-mongodb-config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /PATH_TO_PACKAGE/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead)

Maybe getMongodbMemoryOptions could also check for a jest.config.cjs file? Or the import() function could be used, but then getMongodbMemoryOptions would need to be an async function.

SamTolmay avatar Nov 08 '21 12:11 SamTolmay

The latest version allows you to override this path with the MONGO_MEMORY_SERVER_FILE env var:

https://github.com/shelfio/jest-mongodb/blob/5cbc7cc1169c2a6fdb96a2eafbd9c88661f2d395/helpers.js#L4

However, this obviously doesn't work if you want to run individual tests from your IDE, unless you complement it with another workaround; e.g.: https://stackoverflow.com/questions/32760584/in-intellij-how-do-i-set-default-environment-variables-for-new-test-configurati

I agree jest-mongodb-config.cjs should be used by default when using ESMs.

gnarea avatar Jun 21 '22 09:06 gnarea

Might this issue be why my global.__MONGODB_URI__ is undefined? I'm trying to use @shelf/jest-mongodb in a pure ESM project. Although I'm not getting the error @SamTolmay posted, I'm not able to get the memory server to work.

Please see jest's documentation for this preset.

Thank you.

UPDATE: Whoops, I had let testEnvironment slip by me. Once I removed that, my error changed (and I'm now trying to chase that down).

tim-rohrer avatar Jun 27 '22 17:06 tim-rohrer

Same issue, the workaround of setting the environment variable MONGO_MEMORY_SERVER_FILE to jest-mongodb-config.cjs worked for me. Another solution would be to support the import of ES modules, like jest does (see https://jestjs.io/docs/ecmascript-modules).

ntraut avatar Jun 21 '23 07:06 ntraut

Same Issue. Stupid workaround would be using the enviroment variables of mongodb-memory-server like MONGOMS_VERSION=4.4.5 .

rfehling-mittwald avatar Oct 11 '23 09:10 rfehling-mittwald