How to provide test ESModule implementation in mocha test?

How to provide test ESModule implementation in mocha test?

Problem Description:

I have a mocha test which tests my code. My code uses another "core" npm package (which uses CommonJS modules). One of "core" the files ("synchronizer.js" file) requires "prefs" modules which is assumed to be provided at some point:

const {Prefs} = require("prefs");

In the mocha test i’d like to implement this "prefs" module, so i’ve created "prefs.mjs" file (with exports that "synchronizer.js" expects). However during the test i have the following error:

Error: Cannot find module 'prefs'
Require stack:
- /Users/developer/Documents/dev/src/project/node_modules/core/lib/synchronizer.js
- /Users/developer/Documents/dev/src/project/node_modules/core/lib/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1026:15)
    at Module._load (node:internal/modules/cjs/loader:872:27)
    at Module.require (node:internal/modules/cjs/loader:1092:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/developer/Documents/dev/src/project/node_modules/core/lib/synchronizer.js:26:17)
    at Module._compile (node:internal/modules/cjs/loader:1205:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1259:10)
    at Module.load (node:internal/modules/cjs/loader:1068:32)
    at Module._load (node:internal/modules/cjs/loader:909:12)
    at Module.require (node:internal/modules/cjs/loader:1092:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/developer/Documents/dev/src/project/node_modules/core/lib/index.js:33:24)
    at Module._compile (node:internal/modules/cjs/loader:1205:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1259:10)
    at Module.load (node:internal/modules/cjs/loader:1068:32)
    at Module._load (node:internal/modules/cjs/loader:909:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

My project structure:

sdk
test
     unit
           prefs
                 prefs.mjs
          test.js

I run the tests (package.json):

   "dependencies": {
     ...
     "core": "^0.10.1",
     ...
   },
   "scripts": {
     ...
     "unittest": "mocha --recursive ./test/unit/*.js",
     ...
   }
}

test.js looks as follows:

describe("Section1", function() {
  beforeEach(async function() {
    await configureTestEnv();

    // TODO: set "prefs" module
    // TODO: set "io" module
    // TODO: set "info" module
  });

  describe("subscriptions", function() {
    it("throws if no condition is achieved", async function() {
   ...

I can run the tests (which does not include "core") successfully, so the testing works in general.
I suspect i have to use smth like webpack or babel, but i’d like to keep it clean and not using any deps or intermediate build steps if possible.

Should i pass modules directories paths to mocha to let node.js somehow find it?

Solution – 1

Ended up using webpack and generating a module with aliased modules including prefs.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject