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

285
Visualizações
Test the changed state after useEffect has runned

I use ReactJs, jest and react testing library. I have this code:

const App = ({data}) => {
  const [state, setState] = useState(); // after useEffect runs state should be false
  console.log('test state', state)
  useEffect(() => {
     setState(!data) 
  }, [])
  return <p>'data is' {data},<p/>
}

<App data={12}  />

After the useEffect will run the state should be false. Now i want to test the component using jest and react testing library.

describe('App', () => {

  beforeEach(() => {
    const setValue = jest.fn(x => {});
    React.useState = jest.fn()
      .mockImplementationOnce(x => [x, setValue])
  })
  test('It should render the correct value', async() => {
      const v = utils.queryByText(12)
      expect(v).toBeInTheDocument()
    })
  })
})

When i run the test in console.log('test state', state) i get for first time console.log('test state', undefined) and after that console.log('test state', false), but i need to get only the false value, because due the fact that for the first time the value is undefined the test fails. How to do this?

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

0

You are getting two console logs because this line initialises your state (undefined):

const [state, setState] = useState();

After that the useEffect hook runs onComponentDidMount and changes the state, which causes the component to rerender (second log statement).

You could use something like this, if you want to init the state from the props:

const [state, setState] = useState(!data);

Update:

This works on my machine: source file

test file

result

Maybe you have some typos?

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to wait until the component did mount and then run your expectation. In react testing library, there is a special method for it. waitFor.

from React testing library documentation:

When in need to wait for any period of time you can use waitFor, to wait for your expectations to pass.

import {waitFor} from '@testing-library/react'

describe('App', () => {
  // rest of the codes ...
  test('It should render the correct value', async() => {
      const v = utils.queryByText(12)

      await waitFor(() => {
         expect(v).toBeInTheDocument()
      })
    })
  })
})

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