Eric Stermer
Eric Stermer

Reputation: 1101

Jest/Enzyme document.createRange is not a function on mount

When trying to use ezymes mount on a component that is rendering MaterialUI's Tooltip, there is an error stopping my tests:

TypeError: document.createRange is not a function

Upvotes: 4

Views: 4460

Answers (2)

Nathan5x
Nathan5x

Reputation: 199

For anyone looking Typescript solution

  document.createRange = () => ({
    setStart: jest.fn(),
    setEnd: jest.fn(),
    // @ts-ignore
    commonAncestorContainer: {
      nodeName: 'BODY',
      ownerDocument: document,
    },
  })

Upvotes: 5

Eric Stermer
Eric Stermer

Reputation: 1101

To fix this I defined document.createRange in my ./src/setupTest.js

  document.createRange = () => ({
    setStart: () => {},
    setEnd: () => {},
    commonAncestorContainer: {
      nodeName: "BODY",
      ownerDocument: document,
    },
  })

Upvotes: 13

Related Questions