Standard way to unit test and mock office-js API

Standard way to unit test and mock office-js API
9

Upvotes

Upvote

 Sep 03 2021
6 Comments (6 New)
New

Currently, there is no documentation or any model to follow about unit testing and mocking with Office-js. Using standards approaches like Sinon or Jest proves challenging because office-js API needs to be loaded by an HTML file, making the API not available beforehand.

This issue also increases because some test APIs may require the entire object to be mocked, which can have more than 100 properties, making it not feasible.

There is also the issue of making the tests preserve the order of functions load or sync, which are very common to occur in the development and are difficult to test because stateless test APIs do not support easily adding state variables to handle those functions.

Having an easy and common approach, with an explanation of how to use it, would solve these problems, thus making unit testing/mocking office-js a quicker and easier experience.

Related issues: 

 

We would like to get feedback and suggestions on your experience with unit testing/mocking office.js, as we plan on delivering a solution to help alleviate your problems. Here are some questions that we would like to understand with your feedback:

  1. Do you face difficulties when dealing with mocking office-js API?
  2. What would you like to help easier the unit testing experiences?
  3. What are some pain points you are facing?
Comments
Copper Contributor

We face these same difficulties trying to unit test our code that interacts with Excel.  It stems from the fact that there is no global Office or Excel object available when we're running our test in jest (outside of Office).

 

Currently, we have a Mock object that creates a Mock context for our Excel.run() calls.  From there, we need to create a mock for everything that we call into, which can be quite extensive.  At this point, we're pretty much just mocking the functions that 'create' other objects, so that they create other Mock objects so that we can get unit test coverage through our code.  So, we put in a lot of work just making sure that our code will execute fully in the unit test.

 

But, we're not really doing any useful tests in many of these scenarios.  We would like to be able to do something like:

  • take a raw value
  • assign it to a range
  • set the format on it
  • test that the range is displaying the formatted value that we expect. 

But, unless we go into our mock object and implement all of the formatting code, we can't test it.  We can only test that we have the raw value and format that we expect.  It's not really feasible to implement everything that excel does in a mock, so finding out what is the most useful to test is a major challenge.

Microsoft

Thanks a lot for the comments TimBeese,

I do agree that the approach of creating Dumb Classes work, but it is not very scalable and requires a lot of time just to code it. Also, having to change the mock classes is not ideal as well.

We are working on providing a better and easier experience than that.

Thanks for the suggestions!

Brass Contributor

Hello @TimBeese , I'm currenlty working on the same topic and honestly, I'm not an expert in jest. So, I dont know exaclty what causes my problems and therefore I would like to ask, if you could provide a simple example of the setup you used for the tests?

Microsoft

Hi TDahlem, Microsoft just published a package to help unit testing.
Please check Office Addin Mock that is now the standard way to create unit tests for the Office JavaScript API.

Brass Contributor

Hey @Igor_Ribeiro, oh great thanks. I'm currently a little bit confused that I didn't find it when searching for possible examples. I only found officejs-mock but that did not fit.

Microsoft

@TDahlem and @TimBeese  We now have documentation for unit testing and mocking in add-ins. Please see Unit testing in Office Add-ins