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
Error: Not implemented: HTMLFormElement.prototype.submit #1937
Comments
Yes, indeed, form submission is not implemented. This is working as expected. |
@runbb did you find another solution for this? I am trying to do the same thing. |
@domenic do you have any ideas on how to accomplish this functionality? Perhaps another library? |
@stefanbuck no :( |
@domenic is there a reason why this is unimplemented? Would you be open to a pull request to implement this? |
See https://github.com/jsdom/jsdom#unimplemented-parts-of-the-web-platform. This is about navigation, and thus a huge undertaking. You're welcome to try, but please understand you'd need to be implementing basically all of https://html.spec.whatwg.org/multipage/browsing-the-web.html. |
@domenic it would be nice to disable this error message. |
You can do so; see the readme on jsdomErrors. |
Is it still possible to implement the handling of the inputs when the form is submitted? |
My workaround for now is |
Ran into this when writing tests using Jest.
Added this before I mount my component in the test case. |
I managed to silence the errors by doing: describe('submit', () => {
let emit;
beforeAll(() => {
({ emit } = window._virtualConsole);
});
beforeEach(() => {
window._virtualConsole.emit = jest.fn();
});
afterAll(() => {
window._virtualConsole.emit = emit;
});
it('should handle submit ', () => {
...
});
}); |
There is a nice workaround here https://kula.blog/posts/test_on_submit_in_react_testing_library/ |
Hi! I like your workaround but I am not sure where to do that, you thing you could help maybe? I have the following code: (see I added your "fix" as the first statement in main())
but it is still raising the same |
Possible solution: // Following code mocks window.console.error
// to ignore the "Not implemented: HTMLFormElement.prototype.submit".
//
// Problem: We use "form.onsubmit" event listener in some tests,
// but HTMLFormElement.prototype.submit is not implemented in JSDOM,
// although the tests are passing and handler fires.
//
// More:
// https://github.com/jsdom/jsdom/issues/1937
// https://github.com/facebook/jest/issues/5223#issuecomment-489422244
let origErrorConsole;
beforeEach(() => {
origErrorConsole = window.console.error;
window.console.error = (...args) => {
const firstArg = args.length > 0 && args[0];
const shouldBeIgnored =
firstArg &&
typeof firstArg === 'string' &&
firstArg.includes('Not implemented: HTMLFormElement.prototype.submit');
if (!shouldBeIgnored) {
origErrorConsole(...args);
}
}
})
afterEach(() => {
window.console.error = origErrorConsole;
}) |
If you don't care about the <form onsubmit="return false;"> No more error message |
This solution breaks stacktrace of real error and it'll be harder to find them. |
If you use a virtual console and listen for |
@domenic, could you please show us how to use a virtual console from the test file itself? Let's suggest that the test file is next
P.S: there is jest as a test runner |
Sorry, you'll need to ask Jest for support on how to use Jest. The jsdom is only able to provide support for jsdom itself, not for any package that uses jsdom as a dependency. |
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. |
…lemented: HTMLFormElement.prototype.submit More info: jsdom/jsdom#1937 testing-library/react-testing-library#755
…lemented: HTMLFormElement.prototype.submit More info: jsdom/jsdom#1937 testing-library/react-testing-library#755
…lemented: HTMLFormElement.prototype.submit More info: jsdom/jsdom#1937 testing-library/react-testing-library#755
This workaround will apply to all the unit tests of your project.
// jest.warnings.js
global.originalLogError = global.console.error;
global.console.error = (...args) => {
/**
* Avoid jsdom error message after submitting a form
* https://github.com/jsdom/jsdom/issues/1937
*/
const errorMessage = "Not implemented: HTMLFormElement.prototype.submit";
if (args && args[0].includes(errorMessage)) {
return false;
}
global.originalLogError(...args);
return true;
};
// jest.config.js
module.exports = {
setupFiles: ["./jest.warnings.js"]
}; |
It would be great to add form submission specifically to the list of unimplemented features. I read this list before picking this library and did not associate navigation with form submission. Everything is great besides that! |
comibining @BoGeM's lifecycle tips and @brunogarcia file separation works just fine. // jest.warnings.ts
// see https://github.com/jsdom/jsdom/issues/1937
// @ts-ignore
let origErrorConsole: any;
beforeEach(() => {
origErrorConsole = window.console.error;
window.console.error = (...args: any []) => {
const firstArg = args.length > 0 && args[0];
const shouldBeIgnored =
firstArg &&
typeof firstArg === 'string' &&
firstArg.includes('Not implemented: HTMLFormElement.prototype.submit');
if (!shouldBeIgnored) {
origErrorConsole(...args);
}
};
});
afterEach(() => {
window.console.error = origErrorConsole;
}); // jest.config.ts
const jestConfig = {
preset: 'ts-jest',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
setupFilesAfterEnv: ['<rootDir>/test/setupTests.ts', '<rootDir>/test/jest.warnings.ts],
// other configs...
};
export default jestConfig; |
I only needed this part: beforeEach(() => {
window._virtualConsole.emit = jest.fn();
}); |
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. |
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 it("submits form when button is pressed", () => {
const handleSubmit = jest.fn().mockImplementation((e) => e.preventDefault()); // gets rid of console error
render(<Form onSubmit={handleSubmit} />);
fireEvent.click(screen.getByRole("button")); // if you have multiple buttons on the form, add an aria-label so you can use {name: <aria-label>} here
expect(handleSubmit).toHaveBeenCalledTimes(1);
}); |
The warning message could definitely tell user that they might want to prevent the default behaviour or mock it somehow. |
I use a const onSubmit = jest.fn();
render(<MyComponentWithAForm />, {
wrapper: () => <FormMock onSubmit={onSubmit} />,
});
userEvent.click(scree.getByRole('button'));
expect(onSubmit).toHaveBeenCalledWith({
method: 'post',
action: '/foo-bar',
data: {
foo: 'bar',
},
target: '_blank',
}); |
It works for me too. But it is worth mentioning where you take userEvent. Actually, it's from a separate package. import userEvent from '@testing-library/user-event' |
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 error in the console (although no test failures). See the following link for more information: jsdom/jsdom#1937 (comment)
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: const mockSubmit = jest.fn();
jest.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(mockSubmit); Then I can verify the submit call: expect(mockSubmit).toBeCalled(); |
Error
The text was updated successfully, but these errors were encountered: