Skip to content

@graphql-tools/mock - controllable randomness when mocking out union types #5614

@waynezhang1995

Description

@waynezhang1995

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions