Testing exported function
We will then write the corresponding unit test case for testing the functionality of the exported function as:
Now that we are familiar with exported functions let us move to the next part. Let us first see what the issue would be first.
Non-exported (private) function
A non-exported function would be something like the secret function in this file:
Now, if we were to test for baz in our unit test,
Note: We should generally not separately test private functions. They are a part of the implementation detail. If a private function behaves incorrectly, then the publicly exposed functions which make use of it would misbehave as well. This way, by testing the public interface, we are also testing the private function. But there are scenarios like the private function is too complex where this might be needed. Another scenario could be that multiple private functions are being invoked one after another in a public function, and we need to test them individually.
Now that we know what the problem is, let us think of potential solutions.
The most obvious one would be to not have “secret” as a private function and export it from the module foo. Though it is a quick way of resolving this problem, it is not the right way to do it. We are exposing a lot of functionality for the sake of unit testing our module, which can bring in a lot of security risks with it.
Having written unit-tests in other languages, I knew there should be a better solution out there. I was looking for something that would let me keep the private functions as private members of the module but still make them testable. And Rewire provided me with this functionality.
Though Rewire was introduced as a monkey patching solution for unit-tests, the official description states:
“Rewire adds a special setter and getter to modules so you can modify their behavior for better unit testing. You may:
- inject mocks for other modules or globals like a process
- inspect private variables
- override variables within the module.”
And the second point is what we need to solve our problem!
Rewire and ES6
The common js implementation of rewire does not have ES6 support. And since we are using ES6 imports in our project, we want to use a package that transfers the concepts to ES6. And that is where the babel plugin for rewire comes into play.
babel-plugin-rewire is inspired by rewire.js and transfers the concepts to ES6 using Babel. So we will install it in our project using npm/yarn:
And in the babel config file, we need to import it:
Unit-test a private function
Now that we have Rewire in place using the babel-plugin, we can access the private/non-exported function using the __get__ method. This method on the module acts as a getter allowing us to pull out the private function:
If there is something that you would like to add to the post or something that you think we should cover next, do leave a comment below telling us about it.