score:15

Accepted answer

you have to stub out window.getselection().removeallranges(). i think this would work:

before(() => {
  window.getselection = () => {
    return {
      removeallranges: () => {}
    };
  })
});

score:0

for me, i also need this mock window.document.getselection = jest.fn()

score:7

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

Related Query

More Query from same tag