ts-mockito
ts-mockito copied to clipboard
Allow getting the mock from the instance
instance
allows you to go from mock -> instance, but I need to go the other way.
The reason is that I want to have one part of my test setup my mocks, e.g.:
const dependencies = {
depA = instance(mock(...)),
depB = instance(mock(...)),
depC = instance(mock(...))
}
And then later do when(dependencies.depA.whatever())
but depA
is the instance.
How can I get the mock for a given instance?
I almost filed this issue again b/c I'm looking at ts-mockito again and this is still a blocker. I.e. in test code like:
it("uses a mock", () => {
// ctx / ctx.foo is created with a mock(Foo)
const ctx = getAppContext();
// ctx.foo is an instance so that this works
runBusinessLogic(ctx);
// however since ctx.foo is an instance, the `when` calls fail
when(ctx.foo.doSomething()).thenReturn("asdf");
});
What I'd like to do is:
const mockFoo = mock(ctx.foo);
when(mockFoo.doSomething()).thenReturn("asdf");
I.e. when I call mock
with an existing mock instance, it'd return the mock for that instance, instead of a brand new mock. I believe this could be done without a breaking change...although the mock(clazz?: any)
typing might get in the way.
Alternatively a new method like getMock
would be a cleaner change. Reusing mock
in both "create new mock" and "return mock of instance" might be too cute.