2

Questions

  • What is the correct/acceptable way to access all JavaScript/TypeScript functions in a module for unit testing?
  • Is there a (good) reason why I shouldn't just export all my functions?

Context

Please forgive my ignorance; I have spent the majority of my professional career writing Python code using the Test Driven Development (TDD) approach. Now, I am finding myself learning ReactJS/TypeScript and figuring out how to implement unit tests, but I quickly found out that functions are only accessible if you export them. As most of you know, Python is very permissive; there really is no concept of privacy, so it's just a matter of importing the module and being respectful of what you have access to. But JavaScript only imports the functions of a module that have been explicitly exported, thus providing a modest barrier.

I've been told that it's not a great idea to export all my functions for testing, but I'm not aware of another way to actually be able to test them.

Examples

What I've been told is "correct/better":

Sample1.js:

const func1 = () => {
    //code that does stuff
    return "stuff";
};

const func2 = () => {
   //code that does other stuff
   return "otherStuff";
};

export { func1 };

Sample1.test.js:

// executed via Jest framework

import * as sample from "./Sample1.js"

describe("Unit Tests for Sample1", () => {
    test("Unit Test - func1", () => {
        // code that tests stuff
    };

    test("Unit Test - func2", () => {
        // can't test func2 because it's not exported
    };
};

What I'm doing to be able to test all functions:

Sample2.js:

const func1 = () => {
    //code that does stuff
    return "stuff";
};

const func2 = () => {
   //code that does other stuff
   return "otherStuff";
};

export { func1, func2 };

Sample2.test.js:

// executed via Jest framework

import * as sample from "./Sample2.js"

describe("Unit Tests for Sample2", () => {
    test("Unit Test - func1", () => {
        // code that tests stuff
    };

    test("Unit Test - func2", () => {
        // code that tests other stuff because it's exported
    };
};

For the Record

I've looked around on the internet and Stack Overflow specifically for standards and best practices, but I haven't found much that answers this specific question. The closest I found was this SO question, but it's not really what I'm after.

1 Answer 1

3

If you're truly doing test-driven development (TDD) by writing the test before the System Under Test (SUT), then you can view the tests as an executable specification of the SUT. (I'm sorry if this is already obvious to you, but many people here on Stack Overflow use the term TDD interchangeably with unit testing, and say that they do TDD even though they write the tests after the SUT.)

Thus, the tests describe the API of the SUT. In languages with access modifiers and/or explicit export features, you'll need to make the SUT available to the tests. In JavaScript, this does mean that you'll have to export the API that you want to test. In other languages with access modifiers, this also means that you'll have to give the SUT API public availability (such as for example Java and C#).

If you're developing a library, this will also coincide with the API that's available to client code. In fact, tests are the first (or earliest) client of the SUT.

Once the tests are in place, they serve as a regression test suite for the SUT. Thus, if a test fails, it indicates to you that some part of the exported API is broken. This also implies that that part will be broken for client code that makes use of it.

Should you export all methods, then?

Not necessarily. You may want to keep some methods hidden as implementation details. There can be various good reasons for that. Perhaps you're not sure that the helper method's API is robust, and you want to reserve the ability to change it without breaking client code. Perhaps the helper method has poor encapsulation.

As long as such helper methods are created during the TDD process, they will still be transitively covered by tests even if they are not, themselves, exported.

7
  • Thank you, this is a very informative answer. After reading your response to the other question, my question is now this: is it a universal TDD paradigm to fully expose your functions and then change the access modifier once they pass (correctly)? I ask because I've been approaching it exactly the opposite way, testing all helper functions so that I could be confident in the primary function that ties them all together. Sep 12, 2021 at 5:47
  • @JonathanBelden If you've been doing it the other way around, then what I've described isn't universal 😉 But "fully expose your functions and then change the access modifier once they pass" was not what I described. Sep 12, 2021 at 7:47
  • Ok, but I'm asking - is one way right compared to the other? And, if I understand "Red-Green-Refactor" correctly, access to certain blocks of code can change (via moving them to private helper functions) once they have passed their tests. so is that the "right" way of doing it? I'm not trying to be argumentative... I'm just trying to understand TDD standard conventions a little better, especially since I'm leading the charge for it at my work. Sep 13, 2021 at 4:37
  • 1
    @JonathanBelden I realised, however, that in all my writings on this topic, I'd failed to show a code example. That inspired me to write a new blog post. I hope it helps. Sep 13, 2021 at 5:45
  • 1
    @TysonWilliams I don't consider that a solution - I consider it part of the problem. Sep 16, 2021 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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