'How to stub a constructor from an imported ES6 module?
I would like to stub the constructor of a class within a test file. Say I have the following:
// Foo.js
export class Foo {}
and
// myTestFile.js
import {Foo} from "./Foo.js";
import {functionThatUsesFoo} from "./functionThatUsesFoo.js";
class ReplacedFoo {}
// do something to replace `Foo` with `ReplacedFoo` here...
functionThatUsesFoo();
Is there some magic Object.setPrototypeOf() or Foo.prototype = ReplacedFoo sort of call that I can do to make sure functionThatUsesFoo() uses my stubbed constructor. I'd prefer not to resort to dependency injection.
Most resources I found about this topic were using nodejs require() and a testing library that uses node:vm internally. But I'm using Deno/browser environments where this is not easily available.
Some things I tried:
Foo.constructor = ReplacedFoo.constructorFoo.prototype.constructor = ReplacedFoo.prototype.constructorFoo.prototype = ReplacedFoo.prototypeFoo.prototype = ReplacedFooObject.setPrototypeOf(Foo, ReplacedFoo.prototype)
I made a library to solve situations like these that allows you to replace the contents of imported files. But it has some inevitable limitations so I want to avoid it where possible. Similarly, using import maps to solve this isn't really a satisfying solution either as it doesn't scale well and I might as well use the library at that point.
Solution 1:[1]
with .mjs you basically want to re-work your code to dependency injections everywhere. and functions to double fat arrows :P
const makeFunctionThatUsesFoo = (fooInstance) => (funcAgs) => {
console.log('I feel secure, but stubs stoped working')
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
