gpt4 book ai didi

Jest doesn't see window.location and throwing an error(Jest看不到window.Location并抛出错误)

转载 作者:bug小助手 更新时间:2023-10-25 14:35:17 31 4
gpt4 key购买 nike



I'm writing the script for the browser.
There is no local environments to run it.
Imagine that you have the script and you paste it to the browser console.

我正在为浏览器编写脚本。没有本地环境可以运行它。假设您有脚本,并将其粘贴到浏览器控制台。


I'm using jest to test this script, but jest doesn't see window.location and other browser properties. In matter of fact thats correct because there are no browser properties because it is only js script written in typescript.

我使用JEST来测试这个脚本,但是JEST看不到window.Location和其他浏览器属性。事实上,这是正确的,因为没有浏览器属性,因为它只是用TypeScrip编写的js脚本。


How can I mock it, or tell to jest to treat this code as it is run in the browser?

我如何模拟它,或者告诉JEST像对待在浏览器中运行的代码一样对待它?


更多回答
优秀答案推荐

In your jest.config.js, you should set the testEnvironment to jsdom.

在您的jest.config.js中,您应该将testEnvironment设置为jsdom。


See Jest's documentation on this property.

有关此属性,请参阅Jest的文档。



Example:

示例:


/** @type {import('jest').Config} */
const config = {
// ...
testEnvironment: 'jsdom',
// ...
};

module.exports = config;

更多回答

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