gpt4 book ai didi

jestjs - 如何配置 jest 使用的 jsdom 实例?

转载 作者:行者123 更新时间:2023-12-04 12:05:58 26 4
gpt4 key购买 nike

我遇到过这个问题 Invalid URL is thrown when requiring systemjs in jest test cases

最后的评论之一表明

“通过在 jsdom 中设置引用配置来操纵 jsdom 实例以获得有效的位置/baseURI。”

我想知道有没有办法让我做到这一点?我可以访问 jsdom不知何故来自 jest对象?

最佳答案

在使用需要 url (location.href) 的项目时,我遇到了类似的问题。您可以在配置中使用 testURL 配置 jest。

以下是您可能在 package.json 中放入的内容(如果这是您配置 jest 的方式)。

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

testURL Doc

如果您需要对 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
});

设置.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

关于jestjs - 如何配置 jest 使用的 jsdom 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34927863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com