Mocking `document` in jest

后端 未结 8 1404
我在风中等你
我在风中等你 2020-11-29 04:12

I\'m trying to write tests for my web components projects in jest. I already use babel with es2015 preset. I\'m facing an issue while loading the js file. I have followed a

8条回答
  •  广开言路
    2020-11-29 04:17

    Similar to what others have said, but instead of trying to mock the DOM yourself, just use JSDOM:

    mocks/client.js

    import { JSDOM } from "jsdom"
    const dom = new JSDOM()
    global.document = dom.window.document
    global.window = dom.window
    

    Then in your jest config:

        "setupFiles": [
          "./__mocks__/client.js"
        ],
    

提交回复
热议问题