我在写浏览器的脚本。没有本地环境来运行它。假设您有脚本并将其粘贴到浏览器控制台。我正在使用jest测试这个脚本,但是jest看不到window.location和其他浏览器属性。事实上,这是正确的,因为没有浏览器属性,因为它只是用typescript编写的js脚本。我怎么能嘲笑它,或者告诉jest把这段代码当作在浏览器中运行的呢?
window.location
vdgimpew1#
在jest.config.js中,您应该将testEnvironment设置为jsdom。请参阅Jest关于此属性的文档。
jest.config.js
testEnvironment
jsdom
jest-environment-jsdom
/** @type {import('jest').Config} */const config = { // ... testEnvironment: 'jsdom', // ...};module.exports = config;
/** @type {import('jest').Config} */
const config = {
// ...
testEnvironment: 'jsdom',
};
module.exports = config;
1条答案
按热度按时间vdgimpew1#
在
jest.config.js
中,您应该将testEnvironment
设置为jsdom
。请参阅Jest关于此属性的文档。
jest-environment-jsdom
包。示例: