Error: It looks like you called `mount()` without a global document being loaded

后端 未结 3 1581
借酒劲吻你
借酒劲吻你 2021-01-11 16:44

I\'m trying to mount a component for testing with enzyme, and get this error.

3条回答
  •  逝去的感伤
    2021-01-11 17:17

    npm install --save-dev --save-exact jsdom jsdom-global
    

    Then:

    import 'jsdom-global/register'; //at the top of file, even before importing React
    

    More info here.

    Alternative: In case you are using jest with enzyme-

    In your package.json, specify the test environment for jest as jsdom as follows:

    "jest": {
      "testEnvironment": "jsdom"
    }
    

    I faced the same issue and it worked well for me.

提交回复
热议问题