2016-01-21 5 views

Antwort

0

jsdom ist die Standardumgebung, dass die neueste Version von Jest verwendet, so können Sie die globalen Variablen wie window, document oder location einfach manipulieren.

+1

Von jsdom v11 ', Objekt window.location', nicht konfigurierbar werden und somit nicht verändert werden mit 'Object.defineProperty()' mehr. – Blackus

2

Ich hatte ein ähnliches Problem, wenn Sie ein Projekt verwenden, das eine URL (location.href) benötigt. Sie können jest mit einem testURL in Ihrer Konfiguration konfigurieren.

Hier ist, was Sie in Ihre package.json (wenn das ist, wie Sie jest konfigurieren).

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

testURL Doc

Wenn Sie mehr spezifische Änderungen müssen jsdom können Sie sich und Import installieren jsdom und konfigurieren Sie es getrennt vom Scherz. Hier ein Beispiel:

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's 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 
Verwandte Themen