1

I'm trying to set up a component test with Vue Testing Library and Apollo as described in their example.

import { ApolloClient, InMemoryCache } from '@apollo/client'
import { render } from '@testing-library/vue'
import VueApollo from 'vue-apollo'

const apolloClient = new ApolloClient({
  uri: 'http://localhost:4001/graphql',
  cache: new InMemoryCache({
    addTypename: false,
  }),
})

type ComponentType = Parameters<typeof render>[0]
const renderWithApollo = (Component: ComponentType) =>
  render(Component, undefined, (localVue) => {
    localVue.use(VueApollo)

    return {
      apolloProvider: new VueApollo({ defaultClient: apolloClient }),
    }
  })

However, when I do this, TypeScript yells about defaultClient with the following error:

Type 'ApolloClient<NormalizedCacheObject>' is missing the following properties from type 'ApolloClient<any>': store, writeData, initQueryManager, wsClientts(2739)
apollo-provider.d.ts(16, 5): The expected type comes from property 'defaultClient' which is declared here on type '{ defaultClient: ApolloClient<any>; defaultOptions?: VueApolloComponentOptions<Vue> | undefined; clients?: { [key: string]: ApolloClient<any>; } | undefined; watchLoading?: WatchLoading | undefined; errorHandler?: ErrorHandler | undefined; prefetch?: boolean | undefined; }'

How can the ApolloClient miss those types and how do I give it those properties? It feels like I'm missing configurations.

1 Answer 1

1

I found out I was missing a link property and I had to call provideApolloClient:

import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'
import { render } from '@testing-library/vue'
import fetch from 'cross-fetch'
import { provideApolloClient } from '@vue/apollo-composable'

const apolloClient = new ApolloClient({
  uri: 'http://localhost:4001',
  cache: new InMemoryCache({
    addTypename: false,
  }),
  link: new HttpLink({ uri: '/graphql', fetch }),
})

type ComponentType = Parameters<typeof render>[0]
const renderWithApollo = (Component: ComponentType) =>
  render(Component, undefined, (localVue) => {
    localVue.use(VueApollo)

    provideApolloClient(apolloClient)

    return {
      // @ts-ignore
      apolloProvider: new VueApollo({ defaultClient: apolloClient }),
    }
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.