DEV Community

Testing Non-Exported Functions in JavaScript

Samantha Ming on October 10, 2020

Recently, I finally integrated unit testing into my startup project. I've settled with Jest, I'll speak more about this in a separate journal ent...
Collapse
 
frondor profile image
Federico Vázquez • Edited

Friendly advice, try not to test implementations, rather do it with integrations. It's not worth when you do refractors or even renames and your tests start failing, although the logic and result of your program is still the same.

Collapse
 
samanthaming profile image
Samantha Ming

Fair point— thanks for sharing! 💪

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Wouldn't these non-exported functions really be implementation details that don't really need testing? It doesn't (or shouldn't) matter how the internals work

(What Federico said!)

Collapse
 
leadersheir_ profile image
Warisul Imam

Can you test the functions in a React functional component with this?

Collapse
 
samanthaming profile image
Samantha Ming

Unfortunately, I’m not super familiar with React, so I’m not sure 😓 ...but if you find out, please do share! I’m sure others would love to know as well 😊

Collapse
 
leadersheir_ profile image
Warisul Imam • Edited

Alright, I'll have a go at it and let you know of the outcome.

Collapse
 
ezesunday profile image
Eze Sunday Eze

Just a bit curious. Why did you decide not to export the function?