RichardLitt / knowledge

💡 document everything
MIT License
248 stars 20 forks source link

Exporting methods for unit tests while keeping a single export externally #12

Closed RichardLitt closed 7 years ago

RichardLitt commented 7 years ago

I want to export a single function for a Node package I am writing, so that you don't need to specify a method to use it. However, I want to run unit tests for all of the little functions I am writing around that function, because that's how you do TDD. So, how do I export those little functions, while also exporting the main function?

CF https://github.com/RichardLitt/gh-description/blob/master/index.js

hackergrrl commented 7 years ago

Sometimes I'll put all of the workhorse functions in their own "inner modules" in lib/, and then have a very simple main.js that's something like

var workhorse = require('./lib/workhorse')

module.exports = workhorse

Then you can have your unit tests reference those inner modules, but still only export a single function to downstream.

RichardLitt commented 7 years ago

I've seen that before! Not sure why I didn't notice that that was what it was doing. This makes a lot of sense. Thank you, @noffle.

And thanks for helping me out with these! Nominally, these are for me to answer, but you seem to be particularly adroit at answering them before I have to. 😄

hackergrrl commented 7 years ago

No problem @RichardLitt. :ok_hand: I must've set this repo to "Watched" a while ago, so I get emails for these, and sometimes they're super familiar questions.

RichardLitt commented 7 years ago

Well I hope you don't mind if I keep doing this! You don't have to answer them all. :D

hackergrrl commented 7 years ago

Haha, I'll keep answering as long as I have time and have something to say.