-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tooltip issue using Jest: TypeError document.createRange is not a function #15726
Comments
Please create a minimal repro. It looks like your test setup is incomplete i.e. no DOM is available. |
@MorganDbs See jsdom/jsdom#317. We use the following patch for our tests: global.document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document,
},
}); Does it fix your problem? Update, no longer required since Jest v26.0.0 and jestjs/jest#9606. |
@oliviertassinari Thanks you it's working now !! If I'm understanding well, my test wasn't attached to the BODY, right ? |
@oliviertassinari adding the mock for createRange seem to help, my test is passing, but I get the following console.error: √ without consent, cannot go further (190ms)
console.error node_modules/react-dom/cjs/react-dom.development.js:506
Warning: An update to ForwardRef(Popper) inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
in ForwardRef(Popper) (created by Tooltip)
in Tooltip (created by WithStyles(Tooltip))
.... My test (using it('without consent, cannot go further', () => {
const {getByText} = render(<Registration/>)
const loginButton = getByText(/Log in/)
act(() => {
fireEvent.mouseEnter(loginButton)
})
expect(getByText(/Please accept our terms and conditions/)).toBeInTheDocument()
}) |
@balazsorban44 There are timeouts started when hovering. You should either let all timers run out inside your |
@eps1lon Trying like this: + jest.useFakeTimers()
it('without consent, cannot go further', () => {
const {getByText} = render(<Registration/>)
const loginButton = getByText(/Log in/)
act(() => {
fireEvent.mouseEnter(loginButton)
+ jest.runAllTimers()
})
expect(getByText(/Please accept our terms and conditions/)).toBeInTheDocument()
}) Still the same. 🤔 What do I do wrong? I would prefer this solution.
? |
Yeah sorry this was just a quick guess. Could you ask the question on stackoverflow and link it from here? I'll take a look later. |
UPDATE: The warning goes away in |
Hey! I just found this thread. I am still reproducing this issue on |
The issue should have been solved in jsdom@16.0.0 now. |
I stumbled on this thread as well. As @oliviertassinari suggested, using jest-environment-jsdom-sixteen solved the issue for me. Upgrading jest to v26 works as well as jest uses jsdom v16 as of v26 https://jestjs.io/blog/2020/05/05/jest-26 |
@tedwardmah Updating to jest-environment-jsdom-sixteen fixed the issue for me |
I tried this and fixed my problem, thanks |
Update, since Jest v26.0.0 (jestjs/jest#9606), the workaround in #15726 (comment) is no longer required. |
@oliviertassinari Thanks for sharing, is there any documentation around it, which I can refer to learn more? |
Hi, I'm using Jest and Enzyme to test my component.
My test :
Result: ```
TypeError: document.createRange is not a function
...
console.error node_modules/react-dom/cjs/react-dom.development.js:17117
The above error occurred in the component:
in Portal (created by Popper)
in Popper (created by Tooltip)
in Tooltip (created by WithStyles(Tooltip))
in WithStyles(Tooltip) (created by TextFieldWrapper)
in TextFieldWrapper (created by WrapperComponent)
in WrapperComponent
The text was updated successfully, but these errors were encountered: