Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question .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.

setSponsor(response.data.sponsor); setAddress(response.data.address); setPhaseOfTrial(response.data.phaseOfTrial); } catch(error) { alert(error); })(); }, []);
  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));
  }, []);

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();

I ran into the similar issue.

This issue may due to one of the object next to response.data is undefined. Add conditions to check its presense.

This fixed my issue

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community May 6, 2022 at 16:25 This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Dani3le_ May 11, 2022 at 13:32

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.