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
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
–
–
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.