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.constructor
Foo.prototype.constructor = ReplacedFoo.prototype.constructor
Foo.prototype = ReplacedFoo.prototype
Foo.prototype = ReplacedFoo
Object.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.
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')
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.