How to setup jsdom when working with jest

后端 未结 1 1973
醉话见心
醉话见心 2021-02-04 08:35

I\'m trying to migrate from AVA to Jest. In AVA you can set ava.setup, in which you set the jsdom environment. For example, creating the DOM structure

相关标签:
1条回答
  • 2021-02-04 08:52

    Great question.

    Jest actually ships with jsdom and the environment already configured. You can override it with the testEnvironment setting.

    If you need to set up more aspects of the environment though, you can use the setupTestFrameworkScriptFile setting to point to a file that executes before all of your tests run.

    For example, if you need window.yourVar to be available on the window for all your tests, you would add this to your package.json:

    "jest": {
        "setupTestFrameworkScriptFile": "tests/setup.js"
    }
    

    And in tests/setup.js:

    Object.defineProperty(window, 'yourVar', { value: 'yourValue' });
    
    0 讨论(0)
提交回复
热议问题