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

4

11

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.

Orthopedic answered 11/2, 2022 at 9:22 Comment(0)
K
1

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);
                })
        })();
    }, []);
Kinna answered 20/10, 2023 at 11:18 Comment(0)
E
10

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));
  }, []);
Effusive answered 11/2, 2022 at 9:35 Comment(0)
V
1

You should make your test async since you have async call in your code:

describe('ListInventory', () => {
    it('should render successfully', async() => {
        const { baseElement } = render(<ListInventory />);
        expect(baseElement).toBeTruthy();
    });
});
Vltava answered 18/7, 2022 at 12:42 Comment(0)
K
1

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);
                })
        })();
    }, []);
Kinna answered 20/10, 2023 at 11:18 Comment(0)
S
-1

I think if you use not null operator ! const promise3 = new Promise(function (resolve, reject) { setTimeout(function () { let err = true// this line is causing the erro let err = null;// this is how you can fix it if (!err) { resolve({ name: "billi_alish", age: 34 }) } else { reject(I can't do anytnhign) } }, 2000) }) promise3.then(function (ash) { console.log(ash) console.log("thanks") })

Suzannesuzerain answered 23/4 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.