The modules I want to test and/or mock use some global variables.
For example, the source code of the module makes a reference to a variable session
, which is not defined anywhere but is available at runtime.
Consider the module globals.js
:
let globals = {
contextName: "DATA_PIPELINE",
reportContext: "report",
session: session
};
module.exports = globals;
session
is not defined in this file but available at runtime. It's a global variable of the environment (not under my control).
I notice that with fake timers, you've managed to replace the implementation of global functions such as setTimeout. How could we apply this to any global function/object?
Maybe sinon.replace(object, prop, replacement)
is what you're looking for?
https://sinonjs.org/releases/v7.2.4/sandbox/#sandboxreplaceobject-property-replacement
We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with sinon
, so the bigger community can help answer your questions.
In Node.js, you can do something like:
global.session = sinon.stub();
// Proceed to import the modules that use the session global and run the tests