Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
Unit test form submission not triggered by button outside of form

I'm trying to unit test with React Testing Library a form submission function, however my button to submit the form is outside of the form itself, but is linked via the form=... attribute.

Here is an example test that is failing:

it('my test', () => {
    const formSubmit = jest.fn(() => console.log('/// Form submitted ///'));

    render(<div>
        <form id="test-form" onSubmit={formSubmit}>
            <button type="submit">Inside form</button>
        </form>
        <button form="test-form" type="submit">Outside form</button>
    </div>);

    // Running this line DOES print the console log correctly
    // userEvent.click(screen.getByText('Inside form'));

    // This line DOES NOT print the console log...
    userEvent.click(screen.getByText('Outside form'));

    // Clicking the outside button means this also fails.
    expect(formSubmit).toHaveBeenCalled();
});

Is there something I am missing here?


Update

After a lot of Googling it seems that this issue was caused by me using an older version of JEST for my testing.

It was noted on this GIT issue that there was an issue with older versions of JSDOM that caused this.

This particular issue is likely caused by using older jsdom environments (via jest). The bug was fixed in jsdom/jsdom#2369 which was released in jsdom@12.2.0

It looks like I was using "@testing-library/jest-dom": "^5.12.0" but upgrading to the latest ^5.16.2 doesn't seem to have fixed the issue.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda