1
resposta

Cannot log after tests are done

 PASS  src/App.test.js

Test Suites: 2 passed, 2 total
Tests:       8 passed, 8 total
Snapshots:   1 passed, 1 total
Time:        3.025s
Ran all test suites related to changed files.

Watch Usage: Press w to show more.
  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "Warning: An update to App inside a test was not wrapped in act(...).

    When testing, code that causes React state updates should be wrapped into act(...):

    act(() => {
      /* fire events that update state */
    });
    /* assert on the output */

    This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
        in App (at App.test.js:19)".

      44 |
      45 |   return (
    > 46 |     <div className="App">
         |     ^
      47 |       <header className="App-header">
      48 |         <h1>ByteBank</h1>
      49 |       </header>

      at console.error (node_modules/@jest/console/build/BufferedConsole.js:163:10)
      at printWarning (node_modules/react-dom/cjs/react-dom.development.js:88:30)
      at error (node_modules/react-dom/cjs/react-dom.development.js:60:5)
      at warnIfNotCurrentlyActingUpdatesInDEV (node_modules/react-dom/cjs/react-dom.development.js:23284:7)
      at dispatchAction (node_modules/react-dom/cjs/react-dom.development.js:15656:9)
      at obterSaldo (src/App.js:46:5)

Mesmo problema do chamado anterior, porém as soluções lá contidas não resolvem o problema.

1 resposta

Olá, Euller! Tudo bem?

As chamadas do método render, você pode colocar dentro de uma arrow function no parâmetro da função act (do pacote react-dom/test-utils). Assim, o teste se comportará mais como ocorre no navegador e esses avisos devem desaparecer.

Para fazer isso, você precisa:

Importar a função act:

import { act } from 'react-dom/test-utils';

Passar o render em uma função anônima para o act:

act(() => {
  render(<App />);
})

Espero ter ajudado!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software