02-09-2021 07:59
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-09-2021 07:59
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Answered! Go to the Best Answer.

Accepted Solutions
02-11-2021 10:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-11-2021 10:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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,

02-09-2021 08:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-09-2021 08:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Are you able to put the code into a repository so we can assist?

02-11-2021 09:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-11-2021 09:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

02-11-2021 10:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-11-2021 10:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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,

02-11-2021 10:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-11-2021 10:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

02-11-2021 10:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-11-2021 10:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

02-11-2021 11:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-11-2021 11:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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"
+ }
+ }

