Tags

"ReferenceError: document is not defined" in Jest

Miguel Lima By Miguel Lima
Published on

When you setup Jest, and write down some tests. Sometimes it throws a document is not defined. This means Jest can't get the right environment.

So on your package.json, replace:

{
  ...
  "scripts": {
    ...
    "test": "jest"
    ...
  },
}

With:

{
  ...
  "scripts": {
    ...
    "test": "jest --env=jsdom"
    ...
  },
}

We just solved the ReferenceError: document is not defined throwed by Jest.

Learn something from this article?

We would love to share more content like this with you!

    We won't send you spam. Unsubscribe at any time.