Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Jest is not able to find Fitbit packages while calculating code coverage

ANSWERED

Hey I know this is an old discussion but I'm running into issues. I'm writing an app and attempting to test it. 

 

At a high level (and I can provide more details later):

- typescript

- fitbit-sdk-types

- simple-fitbit-settings

 

Ive setup the mocks

 

__mocks__/simple-fitbit-settings/app.ts

__mocks__/appbit.ts

__mocks__/document.ts

 

When I run the tests for my settings.ts (settings wrapper logic) it's picking up the simple fitbit mock perfectly. 

 

When I run the tests for scenes.ts (a wrapper for document based on fitbit-multiview) it cannot find document.

 

Ive attempted with and without the moduleNameMapper config. I've tried ts and js formats.

 

The only thing I haven't tried is repad in sergius sample.

 

Anyhow. Any insight would be cool. I can post more details if required. 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Thanks for supplying the project. I'm told that Jest doesn't pick up the typedefs from the app component unless you tell it to, as an example:

diff --git a/app/__mocks__/geolocation.ts b/app/__mocks__/geolocation.ts
index 23f5060..0b64839 100644
--- a/app/__mocks__/geolocation.ts
+++ b/app/__mocks__/geolocation.ts
@@ -1,6 +1,6 @@
-export default {
-  geolocation: {
-    watchPosition: jest.fn(),
-    clearWatch: jest.fn(),
-  },
+export const geolocation = {
+  watchPosition: jest.fn(),
+  clearWatch: jest.fn(),
 };
+
+export default geolocation;
diff --git a/package.json b/package.json
index f11d2f2..511fb43 100644
--- a/package.json
+++ b/package.json
@@ -47,6 +47,14 @@
       "js",
       "jsx"
     ],
+    "globals": {
+      "ts-jest": {
+        "tsconfig": "<rootDir>/app/tsconfig.json"
+      }
+    },
+    "moduleNameMapper": {
+      "geolocation": "<rootDir>/app/__mocks__/geolocation.ts"
+    },
     "testRegex": ".*\\.(test|spec)\\.(t|j)sx?$",
     "clearMocks": true,
     "restoreMocks": true,

 

View best answer in original post

Best Answer
0 Votes
6 REPLIES 6

Are you able to put the code into a repository so we can assist?

Best Answer
0 Votes

Here is the bare minimum to replicate the issue: https://github.com/kenjdavidson/fitbit-jest-mocks

 

The app will run and log correctly on the Sense simulator.

The settingsManager tests correctly with the simple-fitbit-settings mock

The positionManager fails due to not being able to find 'geolocation'

 

I'm sure I'm doing something way wrong, hopefully it's not a huge miss on my part.

Best Answer
0 Votes

Thanks for supplying the project. I'm told that Jest doesn't pick up the typedefs from the app component unless you tell it to, as an example:

diff --git a/app/__mocks__/geolocation.ts b/app/__mocks__/geolocation.ts
index 23f5060..0b64839 100644
--- a/app/__mocks__/geolocation.ts
+++ b/app/__mocks__/geolocation.ts
@@ -1,6 +1,6 @@
-export default {
-  geolocation: {
-    watchPosition: jest.fn(),
-    clearWatch: jest.fn(),
-  },
+export const geolocation = {
+  watchPosition: jest.fn(),
+  clearWatch: jest.fn(),
 };
+
+export default geolocation;
diff --git a/package.json b/package.json
index f11d2f2..511fb43 100644
--- a/package.json
+++ b/package.json
@@ -47,6 +47,14 @@
       "js",
       "jsx"
     ],
+    "globals": {
+      "ts-jest": {
+        "tsconfig": "<rootDir>/app/tsconfig.json"
+      }
+    },
+    "moduleNameMapper": {
+      "geolocation": "<rootDir>/app/__mocks__/geolocation.ts"
+    },
     "testRegex": ".*\\.(test|spec)\\.(t|j)sx?$",
     "clearMocks": true,
     "restoreMocks": true,

 

Best Answer
0 Votes

Hey thanks for the reply, I actually tried that (following your other post) and it didn't work either.  When I copied the two files they were still setup without that setting.  After putting it back in I'm still getting the same:

 

```

app/positionManager.spec.ts
● Test suite failed to run

app/positionManager.spec.ts:1:29 - error TS2307: Cannot find module 'geolocation' or its corresponding type declarations.

1 import { geolocation } from "geolocation";

```

 

unable to find geolocation.

Best Answer
0 Votes

With both configurations simple-fitbit-settings/device is still found and mocked well though.  Which is super confusing.  I'm worried that it has to do with the fitbit-sdk-types in that the standard method for setting it up is including it in source (which is odd for type definitions) while I have it configured for typeRoot:

 

- Using the typeRoots:[] / types:[] 

- Including it in includes:[]

 

I've tried both Jest configurations with typeRoots/includes and they both result in the same issue.

 

ps. Sorry about the last response, thought markdown might work, will use the Code link from now on.

 

It's not a massively huge issue, the simulator is working and I can obviously test that way.  

Best Answer
0 Votes

Oh man, I completely missed that one section of added configuration!!

 

This was the missing link!!  I apologize for missing it early.

+    "globals": {
+      "ts-jest": {
+        "tsconfig": "<rootDir>/app/tsconfig.json"
+      }
+    }

 

Best Answer
0 Votes