Search code examples
typescriptjestjsgraphqlapolloapollo-server

How can I test Apollo Server subscriptions using jest?


I was using apollo-testing-library for writing integration tests, It allows me to mutate and query but not subscribe and test. My question is, does anyone have any techniques they have used or know a way to mock a subscription using Jest?


Solution

  • I'm definitely late to the party, and this is not a clear answer. But going through the same hurdle myself I thought someone could find this useful.

    My solution ended up being an End-to-End test of subscriptions.

    Meaning I:

    • Boot up my existing graphql server
    • Create an apollo client in node
    • Create subscriptions in my test

    I've written a sample gist here:

    https://gist.github.com/patrixr/2536ee396d488bd5e38b0278513eefeb