如何在 Jest 中模拟文档并查找元标记?

Эль*_*тов 6 javascript jestjs

我有一个功能,可以设置文档标题和描述,并且我需要编写一个测试。

函数.js

const setMetaTags = function() {
  document.title = "brandName";
  const description = document.querySelector('meta[name="description"]');

  if (description) {
    description.setAttribute('content', 'text text text text');
  }
};
Run Code Online (Sandbox Code Playgroud)

测试.js

import sum from './func.js';

describe('testFunc', () => {
  it('should test something', () => {
    const spyFunc = setMetaTags();


    Object.defineProperty(global.document, 'meta[name="description"]', { value: ?? });
  });
});
Run Code Online (Sandbox Code Playgroud)

???