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

295
Visualizações
Jest.js: Mocking URLSearchParams return value to null fails to push test into falsey test path
jest.spyOn( URLSearchParams.prototype, 'get' ).mockReturnValue( null )

I am expecting this code to cause get to evaluate to null but this doesn't appear to be the case, Istanbul is being used and the code below does not ever evaluate to the falsey condition (shown as a failure to cover ?? '':

    const startDate: string = new URLSearchParams( queryParams ).get( 'start' ) ?? ''
    const endDate: string = new URLSearchParams( queryParams ).get( 'end' ) ?? ''

Any idea what the issue might be?

For context here is the start of the relevant component and test:

// component under test:
const EmployeeTable = () => {
    const queryParams = useLocation().search
    const { id } = useParams<{ id: string }>()
    const employeeId: number = Number( id )
    const startDate: string = new URLSearchParams( queryParams ).get( 'start' ) ?? ''
    const endDate: string = new URLSearchParams( queryParams ).get( 'end' ) ?? ''

// the jest test
    it( 'should render the employee table page section without data', () => {
        jest.spyOn( dataHooks, 'useEmployeeTimeSummaries' ).mockImplementation( (): any => ( {
            data: null,
        } ) )
        jest.spyOn( URLSearchParams.prototype, 'get' ).mockReturnValue( null )
        const { getByTestId } = render( <EmployeeTable />, { wrapper: reactQueryProvider } )
        expect( getByTestId( TEST_ID_EMPLOYEE_TABLE ) ).toBeInTheDocument()
    } )
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