ウェブサイト検索

typescript - TypeError: React.createContextはApolloContextとJestで関数ではありません


私は、Apollo+GraphQLを使っているTS+NextJSアプリで、Jestを使ってTypeScriptコードをテストしようとしています。残念ながら、エラーが発生しています

TypeError: React.createContext is not a function

次のノードモジュールから:

getApolloContext (node_modules/@apollo/client/react/context/ApolloContext.js:7:25)

Stack Overflow の他の質問でこのエラーを見ると、react のバージョンが古いと書かれていますが、私の package.json の react のバージョンは

"react": "^17.0.1",

だから、この問題の原因が何なのかはよくわかりません。どういうわけか、型チェックが失敗しているか、そのノードモジュールでプロジェクト全体のものではなく古いバージョンのReactが使われているようで、これは正しくないように思えます。

これまでに試したことは次の通りです:

Reactとreact-domのノードモジュールを更新中(何も変わらない)

その先に何をすべきか、かなり迷っています。

コードはこちらです:

test.tsx
import * as React from 'react';
import { render } from '@testing-library/react'
import DonePage from '../pages/done'

describe('Pages', () => {
  describe('Index', () => {
    it('Pages render', function testCases() {
        render(<DonePage apolloClient={{}} apolloState={{}}/>)
    })
  })
})

export {}
page.tsx
import * as React from 'react';
import { withApollo } from 'apollo'
{...}

const DonePage: NextPage<{}> = () => {
  return (
    <>
        {...}
    </>
  )
}

export default withApollo(DonePage, { ssr: false })

https://github.com/apollographql/apollo-client/issues/5089 にあるコードが withApollo です

私はjest --runInBandでテストを実行しています