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

205
Visualizações
Mock webserver for tests with Jest

I have a bit of a weird question: I have a program for scraping data from webpages (just the actual HTML pages, not API responses).

I need to write end-to-end tests for this program with the Jest library. In order to test the functionality consistently, I need to make sure that the referred web pages don't change, which would be almost impossible to achieve with resources on the web.

I wondered how good of a practice it is to spin up a new webserver locally with express/fastify/etc. to serve static HTML pages before running the tests and then shut the server down once the tests pass.

Are there any better ways to achieve what I mentioned? Thank you in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For mock server you need npm msw So in your frontend you import it like this:

import {setupServer} from "msw/node";
import {rest} from 'msw'

and after you can create a const server in your function for tests:

const server = setupServer(
    rest.get('/check_signup_email/*', (req, res, ctx) => {
      return res(ctx.json({result: 'ok', emails: []}))
    })
  )
  beforeAll(() => server.listen())
  afterEach(() => server.resetHandlers())
  afterAll(() => server.close())

It is important to close your server after each test and reopened it before new test because if your don't close it differents tests coud be executed on the firsts variables so results could be false.s

about 4 years ago · Juan Pablo Isaza Relatório
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