npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

isograte

v0.1.0

Published

Isolate functions that are only meant to run in production environment. Integrate them back while testing.

Downloads

560

Readme

Isograte

Build Status

Isolate functions that are only meant to run in production environment. Integrate them back while testing.

There are some pieces of code that you only want to run while in the production environment. For eg: a function that sends out emails, or code that makes an HTTP request. We don't want to run those functions while developing or testing.

But we still need to test those functions. Isograte helps to make sure that isolated code only runs when its meant to.

Install

yarn add isograte

# or with NPM
npm install --save isograte

Usage

Isolate

Isolate those functions that you don't want to be running on development or test environment. The isolated function will only run if process.env.NODE_ENV is set to production or we explicitly ask it to run while testing (See Integrate).

import { isolate } from 'isograte'

const sendMail = () => {
  // some code that sends an email
}

// export the isolated function
export default isolate(sendMail)

The exported function is now isolated. If the isolated function is called on environments other than production, it will just return null. You can set the return value when isolating the function. To do that just pass the return value as the second argument to the isolate.


import { isolate } from 'isograte'

const sendMail = () => {
  // some code that sends an email
}

// We can set the return value
export default isolate(sendMail, "Can't send mail in dev env")

But we still need to run this code while testing after we have properly mocked everything. To do that we need to integrate it back.

In sendMail.test.js :

import { integrate } from 'isograte'

// import the isolated sendMail
import { sendMail } from './sendMail'


const mail = {
  // the mail object
}

// we have not yet integrated sendMail.
// It returns null if we are not in production and
// it never calls the original sendMail function.
console.log(sendMail(mail))

// integrate takes an isolated function and
// integrates it back.
// if we invoke integrated function, original
// sendMail will be called.
const _sendMail = integrate(sendMail)
_sendMail(mail)

Note that the function you integrate might be calling other isolated functions. Those functions will still remain isolated and only the function you integrated will be invoked.


Why not just use process.env.NODE_ENV directly?

Let's say you want to test an impure function. You mock everything, You set process.env.NODE_ENV to production and run that code. But there might be other tests running at the same time which expects NODE_ENV to be test. So the code that was not meant to run gets fired. Your function might also call other impure functions. And to test just one function, you need to mock all the functions to get expected results. With Isograte, You can rest assured that the only functions that runs are the ones that you explicitly integrate.