jsdom: Error: Not implemented: HTMLFormElement.prototype.submit
Error
HTMLInputElement {} HTMLInputElement {}
Error: Not implemented: HTMLFormElement.prototype.submit
at module.exports (/home/xlmnxp/Documents/Nodejs/DownFrom99/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at HTMLFormElementImpl.submit (/home/xlmnxp/Documents/Nodejs/DownFrom99/node_modules/jsdom/lib/jsdom/living/nodes/HTMLFormElement-impl.js:68:5)
at HTMLFormElement.submit (/home/xlmnxp/Documents/Nodejs/DownFrom99/node_modules/jsdom/lib/jsdom/living/generated/HTMLFormElement.js:18:21)
at /home/xlmnxp/Documents/Nodejs/DownFrom99/app.js:12:10
at process._tickCallback (internal/process/next_tick.js:109:7) undefined
import { JSDOM } from "jsdom";
JSDOM.fromURL("http://localhost/").then((dom:JSDOM) => {
let document = dom.window.document;
let window = dom.window;
let form = <HTMLFormElement> document.querySelector("form");
let urlField = <HTMLInputElement> document.querySelector("input[name='formvalue']");
let submitButton = <HTMLInputElement> document.querySelector("input[type='submit']");
console.log(submitButton,urlField)
urlField.value = "https://www.youtube.com/watch?v=RWjDLfx8Lww";
form.submit()
})
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 31 (5 by maintainers)
Commits related to this issue
- LPS-124975 Fire submit instead of click to avoid jsdom error: Not implemented: HTMLFormElement.prototype.submit More info: https://github.com/jsdom/jsdom/issues/1937 https://github.com/testing-librar... — committed to boton/liferay-portal by boton 3 years ago
- LPS-124975 Fire submit instead of click to avoid jsdom error: Not implemented: HTMLFormElement.prototype.submit More info: https://github.com/jsdom/jsdom/issues/1937 https://github.com/testing-librar... — committed to 4lejandrito/liferay-portal by boton 3 years ago
- LPS-124975 Fire submit instead of click to avoid jsdom error: Not implemented: HTMLFormElement.prototype.submit More info: https://github.com/jsdom/jsdom/issues/1937 https://github.com/testing-librar... — committed to brianchandotcom/liferay-portal by boton 3 years ago
- Prevent the form from submitting in web components We never want to submit the form this way anyway (ot server-side processing), so we can just turn it off. The form submission was causing a Jest DOM... — committed to mvonballmo/HFU_21H__JAS_20I_app by deleted user 2 years ago
Yes, indeed, form submission is not implemented. This is working as expected.
Ran into this when writing tests using Jest. My workaround:
Added this before I mount my component in the test case. At least it keeps my console clean.
There is a nice workaround here https://kula.blog/posts/test_on_submit_in_react_testing_library/
Just stumbled upon this when writing my own unit tests and think my solution can help someone in the future.
This happens since by default the form attempts to submit the form and refresh the page.
The error can be avoided by simply mocking the submit handler with
event.preventDefault()
:I managed to silence the errors by doing:
My workaround for now is
form.dispatchEvent(new Event('submit'));
If you don’t care about the
submit
you can also disable submitting the form withNo more error message
This workaround will apply to all the unit tests of your project.
jest.warnings.js
file at the root of your project.jest.config.js
:Possible solution:
I only needed this part:
I had a component using a form but not using submit on the form and was still seeing this error. I solved it by adding type=“button” to my other buttons within the form.
Is it still possible to implement the handling of the inputs when the form is submitted?
event.target[name]
are not defined.I use a
FormMock
component to wrap my forms in jest. You can read about it here: https://icatalina.me/article/2021-09-24-error:-not-implemented:-htmlformelement.prototype.submit/This solution breaks stacktrace of real error and it’ll be harder to find them.
Before
After
Today I was testing a component that programmatically submits a hidden form, and I came across this issue when I tried to verify the submit method was called. My fix is as follows:
Then I can verify the submit call:
I was getting this error message in my RTL’s tests. Then I realized I didn’t have put an event.preventDefault() on the submit event handler. After doing it, the message was gone.
If you use a virtual console and listen for
"jsdomError"
s you should get an exception object with a stack trace.