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

152
Visualizações
Testing window.location in jest

I am trying to test the window functionality via jest. I have a function which appends a query param to the url and redirects the page to the other page. A rough snippet of the code is below:

var url = new URL(targetUrl);
    if (typeof status !== undefined) {
      // base encode status
      url.searchParams.append("status", status);
      window.location = url.toString();

I am new to jest and I am facing a bit of difficulty simulating the mock test for this, hence can you please help me with this. Thanks in advance.

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

0

I figured out a way to mock windows.location. I am attaching a code snippet which will be helpful for others who are trying to mock it.

beforeEach(() => {
    delete window.location;
    window.location = Object.assign(new URL("https://dickssportinggoods.com"))
  });

  it('mocks windows.location function', () => {
    //call your function.
    expect(global.window.location).toBe("https://yoururl.com/?status=SUCCESS");
  });
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