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;
更多回答
我是一名优秀的程序员,十分优秀!