Probably my misunderstanding of Testdouble, but I've created this example to illustrate the issue I'm having:
const test = require("ava");
const td = require("testdouble");
const reducer = async (state, event) => {
if (event.id === "123") {
const existing = await state.foo("", event.id);
console.log("existing:", existing);
}
};
test("foo", async (t) => {
const foo = td.func(".foo");
const state = td.object({
foo
});
td.when(foo(td.matchers.anything(), td.matchers.anything())).thenResolve({
id: "123"
});
await reducer(state, {
id: "123",
nickname: "foo"
});
});
This logs: existing: undefined
Whereas I believe it should log: existing: { id: "123" }
as stated by the td.when()
What am I missing?
I think your issue is that
td.object
doesn't work the way you think it does:Source: https://github.com/testdouble/testdouble.js#tdobject
So when you do…
… you're actually mocking
foo
twice.Here's a demo:
Then we can inspect
x.num2str
and we can see that it is the same test double asnum2str
:However
y.num2str
is a completely different test double:I think what you're looking for is the "property replacement" behaviour of
td.replace
:The
z.str2num
function hasn't been mocked:However
z.num2str
is a fully-fledged test double: