You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My needs arose from wanting to test a function that internally created some objects from 3rd party libraries. I want to intercept calls to the constructors of classes in those 3rd party libraries so that I can a) verify that the function being tested actually instantiates those objects with the desired parameters, and b) return fake/mock instances of those objects for testability. I thought that td.replace() is a perfect candidate for this, eg. td.replace(Some3rdPartyClass.prototype, 'constructor'); this worked perfectly with any other methods of classes, I can mock pretty much all methods, static as well as instance, except for 'constructor'. Maybe this was not meant to be but I can't find any documentation that state otherwise.
Issue
I tried to replace the constructors like so:
import { SomeClass } from 'some-library';
import * as td from 'testdouble';
const someMockInstance = {};
const constructor = td.replace(SomeClass.prototype, 'constructor');
td.when(constructor()).thenReturn(someMockInstance);
const instance = new SomeClass();
expect(instance).to.equal(someMockInstance);
This will never pass, and when I ran the debugger, new SomeClass() always invoked the real constructor and not my mock constructor.
Maybe what I tried to do is misguided as there are not many who asked about this out in the wild.
Environment
node -v output: v18.12.1
npm -v (or yarn --version) output: 8.19.2
npm ls testdouble (or yarn list testdouble) version: testdouble@3.16.8
Failing Test
Fork the repo
Add a failing test (probably to the `/regression/src' directory)
Submit a pull request for the failing test or link to your branch here
Example Repo
Create a minimal repository that reproduces the issue
Make sure that a fresh clone can run only npm it and observe the issue
td.contructor() only gives you a fake constructor with which to instantiate mock objects of the target classes, it doesn't replace the constructors of those classes such that when the code under test steps on their constructors that you are notified with td.when() and td.verify(). I did find a solution, assuming there is a package called abc that defines a class Abc, I can hijack the constructor of class Abc as follows: const constructorAbc = td.replace(require('abc'), 'Abc'). Then td.when(constructorAbc(..)).then...() or td.verify(constructorAbc(...)) will be triggered when someone steps on that constructor. That was what I wanted to do but I couldn't find any doc on that.
Description
My needs arose from wanting to test a function that internally created some objects from 3rd party libraries. I want to intercept calls to the constructors of classes in those 3rd party libraries so that I can a) verify that the function being tested actually instantiates those objects with the desired parameters, and b) return fake/mock instances of those objects for testability. I thought that td.replace() is a perfect candidate for this, eg. td.replace(Some3rdPartyClass.prototype, 'constructor'); this worked perfectly with any other methods of classes, I can mock pretty much all methods, static as well as instance, except for 'constructor'. Maybe this was not meant to be but I can't find any documentation that state otherwise.
Issue
I tried to replace the constructors like so:
This will never pass, and when I ran the debugger,
new SomeClass()
always invoked the real constructor and not my mock constructor.Maybe what I tried to do is misguided as there are not many who asked about this out in the wild.
Environment
node -v
output: v18.12.1npm -v
(oryarn --version
) output: 8.19.2npm ls testdouble
(oryarn list testdouble
) version: testdouble@3.16.8Failing Test
Example Repo
npm it
and observe the issueRunkit Notebook
var td = require('testdouble')
at the topCode-fenced Examples
The text was updated successfully, but these errors were encountered: