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

Unit tests using Jest

Hi,

 

If anyone is interested in applying TDD and Unit testing in general, let me know your thoughts about the repo in https://github.com/ihassin/fitbit-coachusa, that shows how to do so locally (using your favourite IDE) and then uploading the code to Fitbit Studio; all this in the hope that one day Fitbit will support a CLI to upload projects and releve us from the UI.

 

Comments welcome, fork it and make it better!

Thanks,

Itamar

Best Answer
7 REPLIES 7

@ihassin: I'll give it a closer look, thanks for sharing. In the meantime, can you add your project in our OSS repository please?

Best Answer
0 Votes

Thank you Fred, I shall add it to that repo under "examples" as showing the bus times is not a true app, but just an example of how to get Jest working with the code.

 

I am looking forward to your and others' comments about my code as I am not an expert in any of this, just wanted to hack my way in to getting tests running in both dev and Fitbit environments.

 

Best Answer
0 Votes

FYI: Expanded testing to "integration"-style tests by mocking out the document class provided by FitBit.

Maybe one day we can have a full mocked library for devs to use for testing 🙂

 

Mock is here, test is here.

Best Answer
0 Votes

@ihassin: I'm happy to tell you that we just release CLI tools.

Best Answer
Fantastic! I’ll give it a try.

Thank you,
- Itamar
Best Answer
0 Votes

With the release of our new CLI SDK tools, Jest integration is super simple. Pop the following in a jest.config.js:

 

module.exports = {

  transform: {

    ".(t|j)sx?": "ts-jest",

  },

  moduleFileExtensions: [

    "ts",

    "tsx",

    "js",

    "jsx",

  ],

  testRegex: ".*\\.test\\.(t|j)sx?$",

  clearMocks: true,

  restoreMocks: true,

  testEnvironment: 'node',

};

 

And add "jest" and "ts-jest" to your devDependencies. "yarn test" should just work then and pick up .test.js files.

 

As for Fitbit APIs you want to mock out, use jest.mock('file-transfer') or similar, and then your actual application code can remain unmodified.

Best Answer

Thanks Liam. All good stuff today 🙂

Best Answer
0 Votes