[testing] Helmet.peek() returns previous elements in unit testing

Author: andrastothtwCreated Jun 17, 2019Updated May 19, 2026

This is a question/bug report.

How can you 'reset' elements put into <head> by Helmet? Helmet.peek() returns elements from previous tests in the same file? See code below:

Code to reproduce

typescript
import * as React from 'react';
import { mount } from 'enzyme';
import Helmet from 'react-helmet';

it('affects the other test :O', () => {
  mount(<div>
    <Helmet>
      <link rel="canonical" href="http://lol" />
    </Helmet>
  </div>);

  const helmetContent = Helmet.peek();
  const canonicalLinkTag = (helmetContent as any).linkTags.find((link: any) => link.rel === 'canonical');

  expect(canonicalLinkTag).toHaveProperty('href', 'http://lol');
});

it('finds the previous as being defined', () => {
  mount(<div>
    Wat
  </div>);

  const helmetContent = Helmet.peek();
  const canonicalLinkTag = (helmetContent as any).linkTags.find((link: any) => link.rel === 'canonical');

  expect(canonicalLinkTag).not.toBeDefined();
});

What is the expected behavior? These two cases should be independent.

Which versions of React and react-helmet...? react-helmet: 5.2.1 react: 16.4.1