How can I configure the jsdom instance used by jest?

前端 未结 4 1154
清歌不尽
清歌不尽 2021-02-13 23:07

I\'ve come up against this issue Invalid URL is thrown when requiring systemjs in jest test cases

One of the last comments suggests

\"manipulate the jsdom instan

4条回答
  •  旧巷少年郎
    2021-02-13 23:49

    If you are using jsdom (ver 11.12.0) without jest (e.g. with ava + enzyme) then you can set url in jsdom config file

    File src/test/jsdom-config.js

    const jsdom = require('jsdom') // eslint-disable-line
    const { JSDOM } = jsdom
    
    const dom = new JSDOM('', {
      url: 'http://localhost/',
      referrer: 'https://example.com/',
      contentType: 'text/html',
      userAgent: 'Mellblomenator/9000',
      includeNodeLocations: true,
      storageQuota: 10000000,
    })
    global.window = dom.window
    global.document = window.document
    global.navigator = window.navigator
    

    AVA settings in package.json

    {
      ...
      "scripts": ...
      ...
      "ava": {
        "babel": "inherit",
        "files": [
          "src/**/*.test.js"
        ],
        "verbose": true,
        "require": [
          "babel-register",
          "ignore-styles",
          "./src/test/jsdom-setup.js",
          "./src/test/enzyme-setup.js"
        ]
      }
    }
    

提交回复
热议问题