Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Jest:- node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */);

I have below code in react.

useEffect(() => {
        (async () => {
            await httpClient
                .get(`${config.resourceServerUrl}/inventory/`)
                .then((response) => {
                    setSponsor(response.data.sponsor);
                    setAddress(response.data.address);
                    setPhaseOfTrial(response.data.phaseOfTrial);
                })
                .catch((error) => {
                    alert(error);
                });
        })();
    }, []);

this code works successfully. only issue is , it is failing in jest test case.

describe('ListInventory', () => {
    it('should render successfully', () => {
        const { baseElement } = render(<ListInventory />);
        expect(baseElement).toBeTruthy();
    });
});

Below is the error.

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[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 "TypeError: Cannot read properties of undefined (reading 'get')".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

I am already using catch block. what wrong is in the code? could you please help me with the same.

like image 863
Shruti sharma Avatar asked Apr 06 '26 16:04

Shruti sharma


2 Answers

Try handling the promise using a try-catch block, as:

  useEffect(() => {
    (async () => {
      try {
        let response = await httpClient.get(
          `${config.resourceServerUrl}/inventory/`
        );
        setSponsor(response.data.sponsor);
        setAddress(response.data.address);
        setPhaseOfTrial(response.data.phaseOfTrial);
      } catch(error) {
        alert(error);
      }
    })();
  }, []);

or

  useEffect(() => {
    (async () => {
        let response = await httpClient.get(
          `${config.resourceServerUrl}/inventory/`
        );
        setSponsor(response.data.sponsor);
        setAddress(response.data.address);
        setPhaseOfTrial(response.data.phaseOfTrial);
    })().catch(e=>alert(e));
  }, []);
like image 54
neeraj tk Avatar answered Apr 08 '26 05:04

neeraj tk


The best way to do this is below:

    useEffect(() => {
        (async () => {
            let response = async () => await httpClient.get(
                `${config.resourceServerUrl}/inventory/`
            );

            response
                .then((res) => {
                    setSponsor(res.data.sponsor);
                    setAddress(res.data.address);
                    setPhaseOfTrial(res.data.phaseOfTrial);
                }).catch((error) => {
                    alert(error);
                })
        })();
    }, []);
like image 40
ahmetcancntrk Avatar answered Apr 08 '26 04:04

ahmetcancntrk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!