A simple module that makes ignoring the node require cache easy.
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.
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
})
})
})
This module is tested in Node versions:
- 6.x.x
- 8.x.x
- 10.x.x
See releases for a detailed changelog.