New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow for deeply nested test double objects #407
Conversation
…ry __mocks__ scratchpad
…/testdouble.js into lgandecki-deeplyNestedProxyObjects
@lgandecki IMO the change to If this is the solution we should use, then I think we ought to extract it into a local utility module with a name like |
I agree. The complexity increases but not for a good reason. I will try to extract parts of it to a utility module. |
Hey @searls , thanks for pushing me here, I found a much better solution: I tried to break this with a bunch of tests, but they all behaved the same way with and without this fix. 'explains a nested td.constructor' () {
function Dog () {}
Dog.prototype.bark = function () {}
Dog.woof = function () {}
var FakeDog = td.constructor(Dog)
const baz = {
FakeDog
}
result = td.explain(baz)
assert._isEqual(result, {
name: null,
callCount: 0,
calls: [],
description: 'This object contains 1 test double function: ["Dog"]',
children: {
FakeDog: {
name: 'Dog',
callCount: 0,
calls: [],
description: 'This test double `Dog` has 0 stubbings and 0 invocations.',
children: {
toString: {
name: undefined,
callCount: 0,
calls: [],
description: 'This is not a test double function.',
isTestDouble: false
},
woof: {
name: 'Dog.woof',
callCount: 0,
calls: [],
description: 'This test double `Dog.woof` has 0 stubbings and 0 invocations.',
children: {
toString: {
name: undefined,
callCount: 0,
calls: [],
description: 'This is not a test double function.',
isTestDouble: false
}
},
isTestDouble: true
}
},
isTestDouble: true
}
},
isTestDouble: true
}
)
},
'passed an object where toString is mocked' () {
const baz = {
toString: td.when(td.function('foo')()).thenReturn('biz'),
bar: () => 'bar'
}
result = td.explain(baz)
assert._isEqual(result, {
name: null,
callCount: 0,
calls: [],
description: 'This object contains 1 test double function: ["foo"]',
children: {
toString: {
name: 'foo',
callCount: 0,
calls: [],
description: 'This test double `foo` has 1 stubbings and 0 invocations.\n\nStubbings:\n - when called with `()`, then return `"biz"`.',
children: {
toString: {
name: undefined,
callCount: 0,
calls: [],
description: 'This is not a test double function.',
isTestDouble: false
}
},
isTestDouble: true
},
bar: {
name: undefined,
callCount: 0,
calls: [],
description: 'This is not a test double function.',
isTestDouble: false
}
},
isTestDouble: true
})
},
'passed an object that contains a nested class that overwrites toString' () {
const somethingWithNestedClass = {
myClass: class Hello {
constructor () {
console.log('hello world')
}
toString () {
return 'hello'
}
}
}
const fakeThing = td.object(somethingWithNestedClass)
result = td.explain(fakeThing)
assert._isEqual(result, {
name: null,
callCount: 0,
calls: [],
description: 'This object contains 1 test double function: [".myClass"]',
children: {
myClass: {
name: '.myClass',
callCount: 0,
calls: [],
description: 'This test double `.myClass` has 0 stubbings and 0 invocations.',
children: {
toString: {
name: undefined,
callCount: 0,
calls: [],
description: 'This is not a test double function.',
isTestDouble: false
}
},
isTestDouble: true
},
toString: {
name: undefined,
callCount: 0,
calls: [],
description: 'This is not a test double function.',
isTestDouble: false
}
},
isTestDouble: true
}
)
} |
landed in 3.11.0 |
pasted from #402
Popular Prisma graphqlgen scaffolds a graphql server with a Context interface, that is used by graphql resolvers to execute commands and queries (on services, repositories, etc).
the default generated example looks like this:
With this change I can do things like:
And everything is still properly typed. :-)
Meaning - if I do
I get
TS2322: Type '{ ids: string; }' is not assignable to type 'User'.
and for
I get
TS2322: Type 'number' is not assignable to type 'string'.
error, with the1
marked in the IDE as an error.for
I get
TS2554: Expected 0 arguments, but got 1.
...and so on.
This context type that holds repositories/services is a common graphql pattern. I'm pretty sure there could be other uses as well.