Coder Social home page Coder Social logo

Comments (5)

venku122 avatar venku122 commented on July 26, 2024 3

You can globally mock react-helmet-async to resolve this issue.

/**
 * Mock helmet module
 */
jest.mock('react-helmet-async', () => ({
  Helmet: () => jest.fn(),
  HelmetProvider: () => jest.fn(),
}));

from react-helmet-async.

thomasjulianstoelen avatar thomasjulianstoelen commented on July 26, 2024 2

Update for 2023:

Using the above solution I encountered the following error:

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

To overcome this just wrap the renderer with the correct providers. I've included <BrowserRouter /> to show that a project might have more than one provider (Redux, Helmet, BrowserRouter and others etc).

// MyComponent.test.js

/**
 * @jest-environment jsdom
 */
import { test, expect, jest } from "@jest/globals";
import React from "react";
import { render } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { HelmetProvider } from "react-helmet-async";

import MyComponent from "src/components/MyComponent.jsx";

const Providers = ({ children }) => {
  HelmetProvider.canUseDOM = false;

  return (
    <HelmetProvider>
      <BrowserRouter>
        {children}
      </BrowserRouter>
    </HelmetProvider>
  );
};

test("renders MyComponent", () => {
  render(<MyComponent />, { wrapper: Providers });
});

from react-helmet-async.

darkowic avatar darkowic commented on July 26, 2024

Sorry @ivan-kleshnin, but after I added the Provider correctly, everything is working fine.

from react-helmet-async.

ivan-kleshnin avatar ivan-kleshnin commented on July 26, 2024

Thanks. I switched to NextJS recently so I don't have time to check back this issue. Closing...

from react-helmet-async.

laneschmidt avatar laneschmidt commented on July 26, 2024

@thomasjulianstoelen thanks for getting me unstuck on this one 🤝

however i encountered another issue. it seems that using this pattern results in an empty dom being rendered:

Ignored nodes: comments, script, style
<body>
  <div />
</body>

could be something unrelated but the code works without HelmetProvider, it just yields that annoying warning.

here is the code:

vi.mock('react-helmet-async', () => ({
  Helmet: () => vi.fn(),
  HelmetProvider: () => vi.fn(),
}));

...

HelmetProvider.canUseDOM = false;
const renderIt = (results: Result) => {
  render(
    <HelmetProvider>
      <BrowserRouter>
        <ResultsStateContext.Provider
          value={{
            results: { Province: 'SomeProvince', ...results },
            setResults: () => ({}),
          }}
        >
          <HiriComponent />
        </ResultsStateContext.Provider>
      </BrowserRouter>
    </HelmetProvider>,
  );
};

from react-helmet-async.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.