Skip to content
Advertisement

Next seo test with react testing library

I am trying to test my SEO component which looks like this:

JavaScript

and my test is like so:

JavaScript

however the test is failing: (it looks like the head element is empty)

enter image description here

Advertisement

Answer

I was having the same issue, but I found this answer on GitHub.

So basically you need to mock next/head, pass document.head to the container property of render‘s options and finally access the document.

Your test would end up like this:

JavaScript

In my case I didn’t test it with that getMeta function but I believe it would work too.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement