Is there an easy way to get the mocked result from addMocksToSchema
#2703
Replies: 2 comments
-
Ah interesting! I don't know any thorough way to get mocked result directly from executable schema without actually executing the query. Getting an hand on the Note though that Also, I'm surprised that react testing library is quite picky with promises created in the test run because I thought that asynchronous setups and teardowns were a pretty standard feature of Jest (that i'm assuming you are using with react testing library). I would drill this down because, if not with graphql mocking, the impossibility to do asynchronous tests might be a blocker in future. |
Beta Was this translation helpful? Give feedback.
-
Hi! I got here because I've followed this pattern https://techblog.assignar.com/dos-and-donts-of-testing-apollo/#do-mock-at-the-service-worker-level- to mock all my responses, which is very handy when you have multiple levels of data coming in that you don't necessarily care about in a specific test, but not so much when you need specific data from a query. I have a query that's been mocked and returns the following: {
user: { details: { name: 'Hello World' } },
items: [
{
id: 'Hello World',
name: 'Hello World',
description: 'Hello World'
},
{
id: 'Hello World',
name: 'Hello World',
description: 'Hello World'
}
]
} now I need to test it when beforeAll(() => {
server.use(
graphql.query(QueryDocument, (req, res, ctx) =>
res.once(
ctx.data({
user: { details: { name: 'Hello World' } },
items: [],
})
)
)
);
}); In this particular case the data is not too bad, but it could become big. if there was a way of doing the following: const mockedData = mockedSchema.getMockedQuery(QueryDocument)
server.use(
graphql.query(QueryDocument, (req, res, ctx) =>
res.once(
ctx.data({
...mockedData
items: [],
})
)
)
); if would be great |
Beta Was this translation helpful? Give feedback.
-
Is there an easy way get the mocked result for query out of executable schema created by
addMocksToSchema
.Context, we are using Apollos
MockedProvider
which expects the mocks in the following format:mocking this by hand is quite painful, also there is no guarantee that the mock is following the schema.
The only way I see so far is to run
graphql
to collect the mocked results and merge them with the passed mocked result for ever query.as the call to
graphql
returns a promise, this will make the test quite hard to write as react testing library is quite picky with promises created in the test run.So my question is, is there any way to get the mocked result directly from executable schema?
Beta Was this translation helpful? Give feedback.
All reactions