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
Is your feature request related to a problem? Please describe.
Hello, we are using @graphql-tools/mock in our unit and functional testing. It works well for most parts; however, we recently discovered that the default mocks for union types are generated randomly, which means the mocked value could change from one test run to another. If an engineer isn't aware that a field is a dependency or even a sub-dependency of their module, it's possible to have a flaky test.
For example, given a union type like
union A = B | C | D;
The resolved type of A is completely unpredictable(can be B, C, or D) between different test runs. We believe this behavior is due to this takeRandom call
Describe the solution you'd like
It would be nice if we can add an extra control to the addMocksToSchema function. Something like fixedUnionType: boolean. If the flag is true, we also return the first subtype from a union and generate mocks for it. In the above example, if fixedUnionType is true, A would always resolve to B.
Describe alternatives you've considered
We could ask our engineers to always provide a mock override when dealing with union types. However, this solution might be unrealistic, even at a small scale, since a component might consume many fields, and providing mocks for everything would be a heavyweight task.
Additional context
The text was updated successfully, but these errors were encountered:
waynezhang1995
changed the title
@graphql-tools/mock - controllable randomness of mocking out union types@graphql-tools/mock - controllable randomness when mocking out union types
Sep 28, 2023
Is your feature request related to a problem? Please describe.
Hello, we are using
@graphql-tools/mock
in our unit and functional testing. It works well for most parts; however, we recently discovered that the default mocks for union types are generated randomly, which means the mocked value could change from one test run to another. If an engineer isn't aware that a field is a dependency or even a sub-dependency of their module, it's possible to have a flaky test.For example, given a union type like
The resolved type of
A
is completely unpredictable(can be B, C, or D) between different test runs. We believe this behavior is due to this takeRandom callDescribe the solution you'd like
It would be nice if we can add an extra control to the
addMocksToSchema
function. Something likefixedUnionType: boolean
. If the flag is true, we also return the first subtype from a union and generate mocks for it. In the above example, iffixedUnionType
is true,A
would always resolve toB
.Describe alternatives you've considered
We could ask our engineers to always provide a mock override when dealing with union types. However, this solution might be unrealistic, even at a small scale, since a component might consume many fields, and providing mocks for everything would be a heavyweight task.
Additional context
The text was updated successfully, but these errors were encountered: