1 year ago
#368469
artberry
variable scope for require
I have a limited JS interpreter for a mobile app, which does not allow usage of module.export
. It has some custom global variables, say, customProp
which are available at runtime, in the interpreter, in the app.
For my tests though I still need to require such JS files and define such variables. Is there a way to pass a mocked value to such custom variables during tests, when call them using require
? A straightforward way would be just to add such variables to the global scope, but with many tests it becomes unusable and very error prone.
A typical script script.js
:
customProp.customFuncCall(); // This works in the engine
A test test.js
:
const { script } = require("./script"); // <- This throws an error: customProp is undefined
I see it is possible to pass a variable to a module when require it, but in that case, the module has to have
module.exports = function (passedVariable) {}
which I cannot do.
javascript
scope
arguments
require
0 Answers
Your Answer