0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ApolloClient】ReferenceError: fetch is not defined でTestが落ちる場合の対処方法

Posted at

概要

NavigationのテストをReact-testing-libraryで実行すると以下のエラーでテストが落ちた。

以下を参考にしてエラーを正常化させたので方法を残す

ReferenceError: fetch is not defined
const createApolloClient = () => {
  return new ApolloClient({
    // ブラウザで実行してる場合はwindowに値がある SSRかCSRかを切り替え
    ssrMode: typeof window === 'undefined',

    // hasuraのAPIリンク
    link: new HttpLink({
      uri: 'https://hoge.hasura.app/v1/graphql'
    }),

    // お決まりの呪文
    cache: new InMemoryCache()
  })
}

対処方法

import 'cross-fetch/polyfill を追加することでテストが正常に通るようになる

+ import 'cross-fetch/polyfill'

const createApolloClient = () => {
  return new ApolloClient({
    // ブラウザで実行してる場合はwindowに値がある SSRかCSRかを切り替え
    ssrMode: typeof window === 'undefined',

    // hasuraのAPIリンク
    link: new HttpLink({
      uri: 'https://hoge.hasura.app/v1/graphql'
    }),

    // お決まりの呪文
    cache: new InMemoryCache()
  })
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?