2

I'm in the process of creating a unit test and I'm trying to optimize it by creating a helper file to replace some repeated code. In doing so some of my expects will end up in this helper file, as it is written now. I'm using the Mocha testing framework and the Chai assertion library.

My questions are:

  1. Is it ok for these expects to be in the helper file?
  2. Is it best practice to keep all expects located within the test file?
1

The concept of a unit test is that that test can run as a self contained test. It will test one behavior is true/false.

Using helper files is fine as long as changes are consistent and won't affect the behavior of previously written tests. In other words, you shouldn't put anything in your helper that is unique to a single test.

| improve this answer | |
  • That makes sense. I'm just thinking from a peer reviewer point of view and for future maintenance. When looking at the test file it may seem as though some test cases are absent when they are in fact in the helper file. I wasn't sure if there was a general best practice for this. In my opinion it would be best to have all test assertions in the main file to quickly view all that's explicitly being tested. – sashak007 Jan 18 '16 at 22:32
  • you want the assertions of the test you want to accomplish in the 'main' file, that is correct. But for example i have an isLoader() in my 'helper' file. This asserts the page loaded and it didnt go to a 404. The distinction is "Test X tests X" the loading of the page is not what X is testing – ECiurleo Jan 19 '16 at 8:31
  • 1
    That's true, I see what saying. I generally err on oversharing, especially when there's many hands potentially sharing the same code base. But it's much more burdensome to recreate functions so all assertions are in the 'main' file - esp when it's not what's being explicitly tested. Thanks for your reply! – sashak007 Jan 24 '16 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.