@forge/bridge breaks tests with mocha

Hi,

long story short: Importing @forge/bridge breaks unit-tests. I seem to remember finding a topic about that issue, but couldn’t locate it anymore.

If this is a duplicate or it is already known, I apologise for the duplicate.

The import:

import {invoke} from '@forge/bridge'

The error:

Error: Cannot find module '@forge/bridge'
Complete Require Stack
Require stack:
- C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\test\views\wizardView.spec.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\test\views\/wizardView.spec.js:6:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module._compile (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.newLoader [as .js] (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at formattedImport (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\mocha\lib\nodejs\esm-utils.js:9:14)
    at Object.exports.requireOrImport (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\mocha\lib\nodejs\esm-utils.js:42:28)
    at Object.exports.loadFilesAsync (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\mocha\lib\nodejs\esm-utils.js:100:20)
    at singleRun (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\mocha\lib\cli\run-helpers.js:125:3)
    at Object.exports.handler (C:\Users\daniel.kleissl\Documents\razzfazz\repos\RnR\plugin\static\wizard\node_modules\mocha\lib\cli\run.js:370:5)

At the moment this means having to work around with programming patterns to avoid importing any file or module that directly references the bridge, which makes writing tests rather inconvenient at times.

We use mocha, chai and sinon as test stack. As far as I am aware there is no way to mock modules BEFORE import using this stack similar to jest’s mock modules. If any of you know another workaround I did not think of I would be grateful.

Also, If this bug is not already known, it would be great if this could be fixed in future versions.

Regards,
Daniel

@QuocLieu Thanks for the effort! I never tried installing it into dev dependencies, as I already have it in my normal dependencies, an I thought that should not change anything. But I’ll try it and let you know.

As for the snippet: That is a very cool idea, but we are using “import” and not “require” statements… and as far as I know imports are evaluated before everything else. So that would mean the import in the file under test gets evaluated, and this throws the “not found” error, even before the line with the stub is reached.
But I could always be wrong about that - I’ll dig around if there is a version of this that works for imports.

Thank you a ton!!

Did @forge/Bridge get updated lately? Because before I couldn’t even import it to stub, but now I got a new error saying it could not find a bridge. Seems it needs a bridge to be defined on the window.__bridge object.

So I just did the following:

setup.js

import sinon from 'sinon';

window.__bridge = {
  callBridge: sinon.stub()
}

I can require that file with mocha using the --require ‘/pathToFile/setup.js’ flag, and now it works! That will make writing future tests and programming soooo much easier! :smiley:

1 Like

Yes sorry my mistake. My initial response is incorrect, theres no need to install @forge/bridge as a dev dep. @forge/bridge@2.5.0 had a missing property causing babel to fail to find the entry point of the module. @forge/bridge@2.5.1 patches this. Glad things are working now :slight_smile:

1 Like