Skip to content

Instantly share code, notes, and snippets.

@sockol
Last active March 6, 2020 21:37
Show Gist options
  • Save sockol/782e4be26dc2ab19cf588b1378c59e34 to your computer and use it in GitHub Desktop.
Save sockol/782e4be26dc2ab19cf588b1378c59e34 to your computer and use it in GitHub Desktop.
Unit test file uploads in your Apollo GraphQL Server
const apolloTesting = require(`apollo-server-testing`);
const {
ApolloServer
} = require(`apollo-server-express`);
// see docs at: https://www.apollographql.com/docs/apollo-server/getting-started/
const serverConfig = {
/*
typeDefs,
resolvers,
context,
*/
}
const testServer = new ApolloServer({
...serverConfig,
});
const client = apolloTesting.createTestClient(testServer);
it(`addFile`, async function() {
try {
const filename = `some-powerpoint.pptx`
const file = fs.createReadStream(resolve(`./tests/${filename}`));
const QUERY = `
mutation addFile($file: Upload!) {
addFile(file: $file) {
id
}
}
`;
const d = await client.mutate({
query: QUERY,
variables: {
file,
},
});
if (d.errors)
throw new Error(d.errors);
assert(d.data.addFile);
} catch (error) {
throw new Error(error);
}
});
@aleccool213
Copy link

What does resolve refer to?

@sockol
Copy link
Author

sockol commented Mar 6, 2020

@aleccool213 require("path").resolve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment