`window` is not being exposed to Jest

后端 未结 6 1555
挽巷
挽巷 2021-02-12 16:50

I have a test that imports a component that in turn imports a helper file that uses the window object to pull out a query string parameter. I get the following erro

6条回答
  •  迷失自我
    2021-02-12 17:19

    https://github.com/facebook/jest/issues/2460#issuecomment-324630534

    It seems like the one of the contributer declared that he is not planning expose jsdom to global under the jest environment.

    However, you could use Object.defineProperty(window, 'location', {value: '…'} API to approach it, like the developer in Facebook do. In your case it could be like:

        Object.defineProperty(window, 'location', {
          value: {
            search: ...
          },
        })
    

    Good luck!

提交回复
热议问题