1
resposta

Teste não passa mesmo tanto certo quando tem 3 ou mais IT dentro do mesmo describe

Estou tento um problema. Quando tenho mais de 2 testes dentro de um describe, recebo um erro:

(node:18955) UnhandledPromiseRejectionWarning: TypeError: Caught error after test environment was torn down

Cannot read property 'body' of null
(node:18955) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 16)
 FAIL  src/App.test.js
  Ao abrir o App
    ✓ Mostrar nome do Banco (10ms)
    ✓ Mostra Saldo (6ms)
    ✕ Mostrar botão Realizar operação (12ms)

  ● Ao abrir o App › Mostrar botão Realizar operação

    TypeError: Network request failed

      at XMLHttpRequest.xhr.onerror (node_modules/whatwg-fetch/dist/fetch.umd.js:473:16)
      at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
      at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
      at XMLHttpRequestEventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
      at XMLHttpRequestEventTargetImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
      at XMLHttpRequest.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
      at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:132:7)
      at Object.dispatchError (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:62:3)
      at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
      at Request.onRequestError (node_modules/request/request.js:877:8)

  console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Error: connect ECONNREFUSED 127.0.0.1:3001
        at Object.dispatchError (/home/douglas/Documents/1976-react-testes/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
        at Request.<anonymous> (/home/douglas/Documents/1976-react-testes/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
        at Request.emit (events.js:201:15)
        at Request.onRequestError (/home/douglas/Documents/1976-react-testes/node_modules/request/request.js:877:8)
        at ClientRequest.emit (events.js:196:13)
        at Socket.socketErrorListener (_http_client.js:402:9)
        at Socket.emit (events.js:196:13)
        at emitErrorNT (internal/streams/destroy.js:91:8)
        at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
        at processTicksAndRejections (internal/process/task_queues.js:84:17) undefined

Test Suites: 1 failed, 1 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        0.425s, estimated 1s
Ran all test suites related to changed files.

Watch Usage: Press w to show more.

Código com 3 it dentro de um describe

import React from 'react'
import { render, screen } from '@testing-library/react'
import App from './App'

describe('Ao abrir o App', () => {
    it('Mostrar nome do Banco', () => {
        render(<App />)

        expect(screen.getByText('ByteBank')).toBeInTheDocument()
    })

    it('Mostra Saldo', () => {
        render(<App />)

        expect(screen.getByText('Saldo:')).toBeInTheDocument()
    })

    it('Mostrar botão Realizar operação', () => {
        render(<App />)

        expect(screen.getByText('Realizar operação')).toBeInTheDocument()
    })
})

Agora, quando eu comento o código de um it que passou no teste, o teste que falhou passa.

import React from 'react'
import { render, screen } from '@testing-library/react'
import App from './App'

describe('Ao abrir o App', () => {
    // it('Mostrar nome do Banco', () => {
    //     render(<App />)

    //     expect(screen.getByText('ByteBank')).toBeInTheDocument()
    // })

    it('Mostra Saldo', () => {
        render(<App />)

        expect(screen.getByText('Saldo:')).toBeInTheDocument()
    })

    it('Mostrar botão Realizar operação', () => {
        render(<App />)

        expect(screen.getByText('Realizar operação')).toBeInTheDocument()
    })
})

Console

 PASS  src/App.test.js
  Ao abrir o App
    ✓ Mostra Saldo (35ms)
    ✓ Mostrar botão Realizar operação (7ms)

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

Watch Usage: Press w to show more.
douglas@RIO-1K2YZ23:~/Documents/1976-react-testes$
1 resposta

Fala ai Pedro, tudo bem? Estranho, olhando os testes não deveria quebrar, os mesmos estão corretos.

Consegue compartilhar o projeto completo? Assim eu consigo simular o problema por aqui e analisá-lo com mais calma.

Pode compartilhar com Github ou Google Drive (zipado).

Fico no aguardo.