How to configure jsdom instance used in jest?

I ran into this problem. Invalid url is called when it is necessary to use system events in jest tests.

One of the last comments suggests

"manipulate the jsdom instance to have the correct location / baseURI by setting the referrer configuration to jsdom."

I am wondering if there is a way to do this? Can I access the jsdom instance from the jest object in any way?

+5
source share
3 answers

I had a similar problem when using a project requiring a URL (location.href). You can configure jokes using testURL in your configuration.

Here is what you can add to your package package.json (if that is the way you configure jokes).

 "jest": { ...other config, "testURL": "http://localhost:8080/Dashboard/index.html" } 

testURL Doc

If you need more specific changes to jsdom, you can install jsdom yourself and import and configure it separately from the joke. Here is an example:

test.js

 'use strict'; import setup from './setup'; import React from 'react'; import { mount } from 'enzyme'; import Reportlet from '../components/Reportlet.jsx'; it('Reportlet Renders', () => { ...some test stuff }); 

setup.js

 import jsdom from 'jsdom'; const DEFAULT_HTML = '<html><body></body></html>'; // Define some variables to make it look like we're a browser // First, use JSDOM fake DOM as the document global.document = jsdom.jsdom(DEFAULT_HTML); // Set up a mock window global.window = document.defaultView; global.window.location = "https://www.bobsaget.com/" // ...Do extra loading of things like localStorage that are not supported by jsdom 
+2
source

jsdom is the default environment used by the latest version of Jest, so you can just manipulate global variables like window , document or location .

0
source

I just went down this road and found out that with Jest 21.2.1, the official way is to fork your own JSDom environment.

This is a little painful to tune, but allows for deep tuning.

Literature:

Environment example: https://github.com/mes/jest-environment-jsdom-external-scripts

0
source

Source: https://habr.com/ru/post/1241145/


All Articles