页面标题不适用于用于测试反应应用程序的开玩笑/酶,如何获取标题



我使用 create-react-app 创建了一个反应应用程序,它产生了一个反应应用程序的准系统,它非常好! 我现在想更改页面的标题(在索引.html中(,然后对此进行测试。我正在使用酶来测试单个组件,这也很棒,但它只提供从组件生成的 html。 如何测试页面/dom,特别是页面的标题?

it('renders App', () => {
  const wrapper = mount(<App />);
  const title = 'test'
  console.log(global.window.document.title)
  expect(wrapper.contains(title)).toEqual(true);
});

global.window.document.title 为空

  1. 您可以在组件/应用程序中设置标题.js以render()componentDidMount()设置为

    document.title = 'titlename'
    

    并在此处检查该标题 expect(global.window.document.title).toBe('titlename') .为我工作!

  2. 尝试使用 react-helmet 。然后检查

    wrapper.find(Helmet).prop("title")).toEqual('titlename');
    

最新更新