Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

A simple module that makes ignoring the node require cache easy.

License

Notifications You must be signed in to change notification settings

crookedneighbor/require-again

Repository files navigation

require-again

Greenkeeper badge

A simple module that makes ignoring the node require cache easy.

Why?

Say you have a file that has slightly different behavior when running in a production environment. Like so:

var isProd = process.env.NODE_ENV === 'production'

if (isProd) {
  // do special prod stuff
} else {
  // do non-prod stuff
}

module.exports = myModule

When you require this module in your test files, it'll run the isProd check once and then cache that version of the module. This makes it difficult to test both behaviors. require-again allows you to get a freshly required version of the module.

Requiring the module normally after using requireAgain will return the original cached version of the module.

Usage

Example using Mocha

var requireAgain = require('require-again')
var pathToModule = './path/to/module'

describe('some test', function () {
  context('prod behavior', function () {
    before(function () {
      this.oldEnv = process.env.NODE_ENV
      process.env.NODE_ENV = 'production'

      this.module = requireAgain(pathToModule)
    })

    after(function () {
      process.env.NODE_ENV = this.oldEnv
    })

    it('does something in a production environment', function () {
      // tests
    })
  })

  context('non-prod behavior', function () {
    before(function () {
      this.oldEnv = process.env.NODE_ENV
      process.env.NODE_ENV = 'dev'

      this.module = requireAgain(pathToModule)
    })

    after(function () {
      process.env.NODE_ENV = this.oldEnv
    })

    it('does something in a development environment', function () {
      // tests
    })
  })
})

Testing

This module is tested in Node versions:

  • 6.x.x
  • 8.x.x
  • 10.x.x

Changelog

See releases for a detailed changelog.

About

A simple module that makes ignoring the node require cache easy.

Resources

License

Stars

Watchers

Forks

Packages

No packages published