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

189
Visualizações
how to mock a post request with MSW in jest

I am new to testing and am currently trying to test a form from my svelte app. On this form there is only one input for a category. When the backend receives this category a uuid is generated for it and a json object with the category and uuid is returned.

it('submits info', async () => {
  render(ItemsForm);
  let requestBody;
  const server = setupServer(
    rest.post(`http://localhost:5025/api/categories`, (req, res, ctx) => {
      requestBody = req.body;
      return res(ctx.status(200), ctx.json({
        name: 'Action',
      }))
    })
  );
  server.listen();
  server.printHandlers()

  const itemInput = screen.getByLabelText('Categories Name');

  await userEvent.type(itemInput, 'Action')
  const submitBTN = screen.queryByRole('button', {name: `Save Categories`})
  await userEvent.click(submitBTN);
  server.close();
  expect(requestBody).toBe
  ({
    name: 'Action',
    
  })
  
})

when I run the test the error that i receive is

expect(received).toBe(expected) // Object.is equality

    Expected: {"name": "Action"}
    Received: undefined

      64 |   await userEvent.click(submitBTN);
      65 |   server.close();
    > 66 |   expect(requestBody).toBe
         |                       ^
      67 |   ({
      68 |     name: 'Action',
      69 |     

I have tried various approaches and tried to search for online resources to find a solution but have not been able to make progress towards a solution.

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