use*_*596 7 jsdom systemjs jestjs
我遇到了这个问题在jest测试用例中需要systemjs时会抛出无效的URL
最后的评论之一表明
"通过在jsdom中设置referrer配置来操纵jsdom实例以获得有效的位置/ baseURI."
我想知道我有办法做到这一点吗?我可以jsdom
从jest
对象以某种方式访问实例吗?
在使用需要 url (location.href) 的项目时,我遇到了类似的问题。您可以在配置中使用 testURL 配置 jest。
以下是您可能在 package.json 中放入的内容(如果这是您配置 jest 的方式)。
"jest": {
...other config,
"testURL": "http://localhost:8080/Dashboard/index.html"
}
Run Code Online (Sandbox Code Playgroud)
如果您需要对 jsdom 进行更具体的更改,您可以自己安装 jsdom,并与 jest 分开导入和配置。下面是一个例子:
测试.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
});
Run Code Online (Sandbox Code Playgroud)
设置.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
Run Code Online (Sandbox Code Playgroud)
jsdom 是最新版本 Jest 使用的默认环境,因此您可以简单地操作window
、document
或 等全局变量location
。
归档时间: |
|
查看次数: |
5803 次 |
最近记录: |