Skip to content

Make private functions available for unit testing

Notifications You must be signed in to change notification settings

SutroOrg/testify

Repository files navigation

testify

npm (scoped) GitHub Workflow Status node-lts (scoped)

Sometimes you have a function in a file that is private, but needs to be tested.

Exporting it into a production environment can result in pierced abstractions, which defeats the purpose of abstraction.

testify allows you to export a wrapped version of a private function. While the function is still exported, attempts to use it in production will result in a thrown Error.

Example

// library.ts
const privateFunction = () => {
    // Something happens here
};

const _testableFunction = testify(privateFunction);

export {_testableFunction};

_testableFunction can be used in your tests (i.e. when process.env.NODE_ENV === 'test'), but will throw in production.

Why not use rewire?

The rewire package approaches this problem via monkey-patching. The problem with that is that you lose visibility of code coverage on your rewired functions.

This approach allows you to get the coverage data you need.

About

Make private functions available for unit testing

Resources

Stars

Watchers

Forks

Packages

No packages published