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

264
Visualizações
Promise test case failing only for Node 16

We are currently upgrading from Node v12 to Node v16.

After the node version update, the following test is failing with this error.

[UnhandledPromiseRejection: 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(). The promise rejected with the reason "Error".] { code: 'ERR_UNHANDLED_REJECTION'

But why does a node version cause this.

This error stops if I remove the .catch() from the test case.
But it is still incorrect outcome cos the snapshot no longer matches if I do that.

This is the test.

it('should test', async () => {
  const thePromise = Promise.resolve({
    json: () => {
      Promise.reject(new Error());
    },
  });

  // validateData is fetch call. 
  validateData.mockReturnValueOnce(thePromise);

  const rendered = renderModule({ state: { code: 'mockRefCode' } });
  rendered.find('#btn').simulate('click');
  await ((thePromise)).then().catch(); // If i remove this catch, above error gone but wrong snapshot (null snapshot).
  expect(rendered.find('#error')).toMatchSnapshot();
});

This is the code being tested.

export const validateCode = async (env, code) => fetch(`www.getapiresponse.example`);

export const SomeReactComponent(props) {
  …
  const [validatingCode, setValidatingCode] = useState(false);
  const [validationError, setValidationError] = useState('');

  const validateData = () => {
    if (!code) {
      setValidationError(langPack.emptyField);
      return;
    }

    setValidatingCode(true);
    validateCode(environment, code.trim())
      .then((response) => response.json())
      .then((body) => {
        if (body.response_status_code !== '200') {
          // some logging
        } else {
          setValidationError('');
        }
      })
      .catch(() => setValidationError(langPack.serviceError))
      .finally(() => setValidatingCode(false));
  };
    
  …
}

If I remove the .catch(), the snapshot ends up as null.

The correct snapshot should be something like following:

- <div
-   className="error"
-   id="error"
- >
-   service
- </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this :

try {
  await thePromise
} catch (e) {
  e.toBeInstanceOf(Error)
}

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