# How to write unit tests using Jest Framework with Angular code?

**URL:** <https://community.cumulocity.com/t/how-to-write-unit-tests-using-jest-framework-with-angular-code/1009>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [August 11, 2023, 8:40am UTC](https://community.cumulocity.com/t/how-to-write-unit-tests-using-jest-framework-with-angular-code/1009 "2023-08-11T08:40:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranjithkumar\_m](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@ranjithkumar\_m](https://community.cumulocity.com/u/ranjithkumar_m)\
**Post date:** [August 11, 2023, 8:40am UTC](https://community.cumulocity.com/t/how-to-write-unit-tests-using-jest-framework-with-angular-code/1009/1 "2023-08-11T08:40:55Z")

</div>

Hi Team,  
I have implemented more of the test cases using the Jest framework with Angular code. But I couldn’t cover the below-mentioned code. While I’m writing the test case, I’m getting the error Fetch method is not a function.

Could you please provide the solution?

**Method : \*\*  
 async getManageObject(): Promise {  
\*\* const baseUrl = `inventory/managedObjects`;**  
\*\* try {\*\*  
\*\* const response = await this.fetchClient.fetch(baseUrl, {\*\*  
\*\* method: “GET”,\*\*  
\*\* headers: { “Content-Type”: “application/json”}\*\*  
\*\* });\*\*  
\*\* return await response.json(); \*\*  
\*\* } catch (error) {\*\*  
\*\* console.error(error);\*\*  
\*\* return null;\*\*  
\*\* }\*\*  
\*\* }\*\*

**Unit test case:**

\*\* it(‘getManageObject Method: getManageObject method API call return data’, fakeAsync(() =\> { \*\*  
\*\* let response :any={\*\*  
\*\* “managedObjects”: [\*\*  
\*\* {\*\*  
\*\* “name”: “document”,\*\*  
\*\* “self”: “[https://inventory/managedObjects/123456](https://inventory/managedObjects/123456)”,\*\*  
\*\* “id”: “123456”,\*\*  
\*\* “c8y\_IsBinary”: “”,\*\*  
\*\* “length”: 498369,\*\*  
\*\* “contentType”: “multipart/form-data; boundary=--------------------------87651329938”\*\*  
\*\* }\*\*  
\*\* ]\*\*  
\*\* };\*\*  
\*\* // let spy = jest.spyOn(mockUserManualService, ‘getManageObject’).mockReturnValue(Promise.resolve(response));\*\*  
\*\* mockUserManualService.getManageObject();\*\*  
\*\* //expect(spy).toHaveBeenCalled();\*\*  
\*\* }));\*\*

TypeError: this.fetchClient.fetch is not a function  
at DocumentService. (C:\Cockpit-UI\custom-widgets\Document\service\document-service.ts:19:47)  
at Generator.next ()  
at C:\Cockpit-UI\custom-widgets\Document\service\document-service.ts:1795:41  
at new ZoneAwarePromise2 (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:1351:25)  
at Object..\_\_awaiter (C:\Cockpit-UI\custom-widgets\document\service\document-service.ts:1738:10)  
at DocumentService.getManageObject (C:\Cockpit-UI\custom-widgets\Document\service\document-service.ts:1834:12)  
at C:\Cockpit-UI\custom-widgets\Document\component\document-hub.component.spec.ts:128:31  
at fakeAsyncFn (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:5637:34)  
at \_ZoneDelegate2.invoke (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:409:30)  
at ProxyZoneSpec2.onInvoke (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:3830:43)  
at \_ZoneDelegate2.invoke (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:408:56)  
at Zone3.run (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:169:47)  
at Object.wrappedFunc (C:\Cockpit-UI\node\_modules\zone.js\bundles\zone-testing-bundle.umd.js:4330:34)  
at Promise.then.completed (C:\Cockpit-UI\node\_modules\jest-circus\build\utils.js:391:28)  
at new Promise ()  
at callAsyncCircusFn (C:\Cockpit-UI\node\_modules\jest-circus\build\utils.js:316:10)  
at \_callCircusTest (C:\Cockpit-UI\node\_modules\jest-circus\build\run.js:218:40)  
at processTicksAndRejections (internal/process/task\_queues.js:93:5)  
at \_runTest (C:\Cockpit-UI\node\_modules\jest-circus\build\run.js:155:3)  
at \_runTestsForDescribeBlock (C:\Cockpit-UI\node\_modules\jest-circus\build\run.js:66:9)  
at \_runTestsForDescribeBlock (C:\Cockpit-UI\node\_modules\jest-circus\build\run.js:60:9)  
at run (C:\Cockpit-UI\node\_modules\jest-circus\build\run.js:25:3)  
at runAndTransformResultsToJestFormat (C:\Cockpit-UI\node\_modules\jest-circus\build\legacy-code-todo-rewrite\jestAdapterInit.js:170:21)

---

<div class="post-metadata">

**Author:** ![Tristan\_Bastian](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/tristan_bastian/32/18259_2.png) [@Tristan\_Bastian](https://community.cumulocity.com/u/Tristan_Bastian)\
**Post date:** [August 11, 2023, 10:23am UTC](https://community.cumulocity.com/t/how-to-write-unit-tests-using-jest-framework-with-angular-code/1009/2 "2023-08-11T10:23:41Z")

</div>

Hi,

For unit tests you would want to mock the requests/responses from external systems instead of actually calling `fetch` here.

In case you want to use services via dependency injection you would either need to include the module that provides the service. Otherwise you would have to manually provide it or a mock of it.

Regards,  
Tristan

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [February 7, 2024, 10:23am UTC](https://community.cumulocity.com/t/how-to-write-unit-tests-using-jest-framework-with-angular-code/1009/3 "2024-02-07T10:23:54Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
