Last active
December 13, 2021 20:34
-
-
Save sockol/75fa00c7f0518491bec1565389125891 to your computer and use it in GitHub Desktop.
Unit test file uploads in your Apollo GraphQL Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: new Promise(resolve => resolve({ | |
createReadStream: () => file, | |
stream: file, | |
filename: filename, | |
mimetype: `application/vnd.openxmlformats-officedocument.presentationml.presentation`, | |
})), | |
}, | |
}) | |
if (d.errors) | |
throw new Error(d.errors) | |
assert(d.data.addFile) | |
} catch (error) { | |
throw new Error(error) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, helped me a lot with figuring out things after upgrading packages.