jsdom补充document的环境
const jsdom = require("jsdom"); const {JSDOM} = jsdom; const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`, { url: 'https://www.tiktok.com/' }); window = dom.window; window.localStorage = {} document = window.document; localStorage = window.localStorage;
完美解决localStorage的问题
localStorage的原理本来就是设置本地键值
因此在jsdom补充url的属性就可以了
参考地址:https://zhuanlan.zhihu.com/p/130969994