Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SamTolmay opened this issue Nov 8, 2021 · 4 comments
Open

Comments

@SamTolmay
Copy link

SamTolmay commented Nov 8, 2021

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.

@gnarea
Copy link

gnarea commented Jun 21, 2022

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

const configFile = process.env.MONGO_MEMORY_SERVER_FILE || 'jest-mongodb-config.js';

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.

@tim-rohrer
Copy link

tim-rohrer commented Jun 27, 2022

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).

@ntraut
Copy link

ntraut commented Jun 21, 2023

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).

@rfehling-mittwald
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants