我已经使用 create-react-app 创建了一个 React 应用程序,它已经生成了一个 React 应用程序的准系统,非常好!我现在想更改页面的标题(在 index.html 中)然后进行测试。我正在使用 enzyme 来测试单个组件,这也很棒,但它只提供从组件生成的 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 为空
最佳答案
您可以在
render()
或componentDidMount()
中将 component/App.js 中的标题设置为document.title = 'titlename'
并在此处检查该标题
expect(global.window.document.title).toBe('titlename')
。对我有用!尝试使用
react-helmet
。然后检查wrapper.find(Helmet).prop("title")).toEqual('titlename');
关于html - 用于测试 React 应用程序的 jest/enzyme 不提供页面标题,如何获取标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097366/