Open mikecfisher opened 5 years ago
Ok I was able to properly mock it by doing this.
describe("Queries", () => {
it("fetches an author stream header", async () => {
// create an instance of ApolloServer, while reusing
// existing dataSources, resolvers, and typeDefs.
// This function returns the server instance as well as our dataSource
// instances, so we can overwrite the underlying fetchers
// APIDataSource is an uninvoked class instance
const { server, APIDataSource } = constructTestServer();
// mock the datasources' underlying fetch methods
APIDataSource.prototype.get = jest.fn(() => mockServerAuthorResponse);
// use our test server as input to the createTestClient fn
// This will give us an interface, similar to apolloClient.query
// to run queries against our instance of ApolloServer
const { query } = createTestClient(server);
const res = await query({
query: GET_AUTHOR,
variables: { username: "amy87" },
});
expect(res).toMatchSnapshot();
});
});
Why not use something like https://github.com/aexmachina/factory-girl ? mocking calls to the database for an integration tests or even testing the resolvers seems like a red flag. What if the queries in the UserAPI aren't right? And then if the User model is changed you have to change the mock everywhere.
I followed the integration tests example pattern in my own app.
However I noticed it was still making api calls to the rest endpoint. I then cloned this repo turned off my wifi and ran the integration tests and sure enough I got an error.
This tells me that
launchAPI.get
is not being properly mocked. Which I think would make sense as there is nolaunchAPI.get
however there is alaunchAPI.prototype.get
however that doesn't seem to work when I change the tests to that.Any ideas?