Enzyme Jest window.getSelection() does not work
You have to stub out window.getSelection().removeAllRanges()
. I think this would work:
before(() => {
window.getSelection = () => {
return {
removeAllRanges: () => {}
};
})
});
for me, I also need this mock window.document.getSelection = jest.fn()
2020 Update
I was struggling with this as well and @Mohammad comment pointed me in the right direction, so I decided to add that as an answer here to help others:
As mentioned by @Mohammad, jsdom
is now on version 16, which supports getSelection
. However, Jest@25
is still using older versions of jsdom
, so what you can do is use this NPM package to "by pass" that:
https://www.npmjs.com/package/jest-environment-jsdom-sixteen
After the install, just update your Jest
config to use this:
{
"testEnvironment": "jest-environment-jsdom-sixteen"
}
Or, use it as a flag direct in the NPM command:
npm run test --env=jsdom-sixteen