question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Popper.js in Jest / JSDOM?

See original GitHub issue

CodePen demo

https://codepen.io/pen?template=wGqJEz

Steps to reproduce the problem

  1. Run UI tests with jsdom
  2. Get exception TypeError: document.createRange is not a function

What is the expected behavior?

Not to get an exception

What went wrong?

Obviously this is not popper’s fault, but maybe it can be fixed here.

Any other comments?

Thanks for the awesome library, I hope this doesn’t come across as disrespectful. The jsdom issue gives the feeling that this won’t implemented anytime soon. I would like to know more about why createRange is used and can we replace it with something else or fall back to another solution if the browser doesn’t support it.

Browsers newer than IE8 already support this so I can’t find another use case other than testing with jsdom.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:20
  • Comments:42 (14 by maintainers)

github_iconTop GitHub Comments

45reactions
jptissotcommented, Jul 24, 2018

I was having the same issue with enzyme mount() and mocking did not work. However, adding this to my setupTests.js file made everything work.


// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

if (global.document) {
  document.createRange = () => ({
    setStart: () => {},
    setEnd: () => {},
    commonAncestorContainer: {
      nodeName: 'BODY',
      ownerDocument: document,
    },
  });
}
43reactions
brentertzcommented, Nov 2, 2017

Just to close the loop on this…

I also discovered that following works and may be more desirable for some folks.

Create a folder/file called __mocks__/popper.js.js adjacent to your node_modules directory containing the following code. Jest will pick it up automatically.

import PopperJs from 'popper.js';

export default class Popper {
  static placements = PopperJs.placements;

  constructor() {
    return {
      destroy: () => {},
      scheduleUpdate: () => {}
    };
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Popper.js in Jest / JSDOM? · Issue #478 - GitHub
Yes as I said, Popper.js is probably never going to work in JSDOM, it relies on real browser DOM APIs to work.
Read more >
Loading popper.js in Jest
First we followed the instruction on popper.js on How to use Popper.js in Jest, which provides two ways of mock the library.
Read more >
How to use Reakit popover in jest / jsdom? · Reakit
I'm wondering, how I could mock popper.js, when it's not a direct dependency for me. I'm using Reakit Popover in one of my...
Read more >
popperjs-final - npm
Start using popperjs-final in your project by running `npm i popperjs-final`. There are no other projects in the npm registry using ...
Read more >
document.createRange is not a function` error while testing ...
2 Answers 2 · This is a good thing to mention, however using Enzyme instead of React-Testing-Library, I did not have to mock...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found