kenjdavidson / fitbit-jest-mocks

Sample application attempting to get Fitbit/Jest mocking working

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fitbit Jest Mocks

Demonstration project with regards to getting Fitbit setup with Typescript and Jest.

Fitbit SDK Types

The project is configured with fitbit-sdk-types which seems like the standard for Fitbit Typescript.

Community

The following discussions/posts were related (and followed) with regards to setting up the Jest environment:

Keys

Due to the way in which fitbit-sdk-types works, in that it needs to be included in the individual project tsconfig files:

// ./tsconfig.json
{
  "extends": "./node_modules/@fitbit/sdk/sdk-tsconfig.json",
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [
      "node",
      "jest"
    ]
  },
}
// ./app/tsconfig.json
{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "typeRoots": [
          "../node_modules/@types",          
        ],
        "types": [
          "node",
          "jest",
        ]
      },
	"include": ["**/*.ts", "../node_modules/fitbit-sdk-types/types/device"]
}

where the app tsconfig extends the project tsconfig. Jest must be configured to look at the app specific version:

// package.json
  "jest": {
    "transform": {
      ".(t|j)sx?": "ts-jest"
    },
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx"
    ],
    "globals": {
      "ts-jest": {
        "tsconfig": "<rootDir>/app/tsconfig.json"
      }
    },
    "moduleNameMapper": {
      "appbit": "<rootDir>/app/__mocks__/appbit.ts",
      "geolocation": "<rootDir>/__mocks__/geolocation.ts"
    },
    "testRegex": ".*\\.(test|spec)\\.(t|j)sx?$",
    "clearMocks": true,
    "restoreMocks": true,
    "testEnvironment": "node"
  }

where the important lines are:

  • moduleNameMapper required configuration for only Fitbit SDK imports. Using node_module modules or definitions (like simple-fitbit-settings) does not require this
  • globals/ts-jest/tsconfig required to point at the individual app tsconfig file. To ensure that the types are available from the includes:[] instead of the types:[].

About

Sample application attempting to get Fitbit/Jest mocking working


Languages

Language:TypeScript 93.6%Language:CSS 6.4%