Microsoft MakeCode

Testing an extension

Hello,
I have come a long way with the project thanks in a large part to this and the Slack community. Once I get the piece fine-tuned, it seems I will need to run some tests on it in order to keep it up-to-date with the MakeCode software. Can anyone point me toward some resources to help me learn how to Unit Test for MakeBlock and the micro:bit?

What process do you all go through? Thank you.
~Mr. R^2

1 Like

I thought I would post an update for those interested.
Here are the answers to most of my Unit Test questions.

  1. What platform/ extension do we use to test our extension?
    A: Microsoft uses Github’s built-in test suite. You could use something else on the command line, but I currently do not see it as necessary.

  2. How do we ensure our tests pass?
    A: If you are using the online version of MakeCode combined with GitHub, this happens automatically. All you need to do is put your test code in test.ts, and the GitHub agent runs through the tests on build.

  3. How do I know what to put in my test code?
    Simply test all of your blocks as you feel they will be utilized.

  4. My library relies upon hardware and other libraries, how is that going to work with Unit Testing?
    It seems to work fine. I just put code in my test.ts file that mimicked how users would use it. It seems all libraries are accounted for (when a user installs your library, it pulls in dependencies automatically).

  5. How do you handle different options and user logic?
    I think this could get me deep into the weeds, so my plan is to use a state machine and run through the primary use cases ensuring all blocks are used effectively.

This is really cool. Thank you all for your help.

Thanks for sharing! Here are how I’m doing some unit testing:

  1. I use an assert() function that tests return values of reporter blocks (function returns). It also totals the successes and failures. Fortunately, everything I’ve needed to test so far can be tested this way.
  2. I have a bunch of individual test cases that do a primitive setup and test. They report outcomes to serial, which works well for how I test.
  3. I’m working from the command line / locally on all development). I usually run the tests (now on both v1 and v2) before I pxt bump.

Here’s my example tests for my time & date extension: https://github.com/bsiever/microbit-pxt-timeanddate/blob/master/test.ts