@everymundo/cleanrequire
Advanced tools
Comparing version 1.0.0 to 1.1.0
12
index.js
@@ -5,2 +5,4 @@ 'use strict'; | ||
const cleanrequire = (libpath) => { | ||
if (!/^[./]/.test(libpath)) return removeCacheAndLoad(libpath); | ||
const | ||
@@ -14,6 +16,12 @@ err = new Error(''), | ||
delete require.cache[require.resolve(libFile)]; | ||
return require(libFile); | ||
return removeCacheAndLoad(libFile); | ||
}; | ||
const removeCacheAndLoad = (libFile) => { | ||
const resolved = require.resolve(libFile); | ||
if (resolved in require.cache) delete require.cache[resolved]; | ||
return require(resolved); | ||
}; | ||
module.exports = cleanrequire; |
{ | ||
"name": "@everymundo/cleanrequire", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Cleans nodejs require.cache for a given module and requires it again. Very useful for testing purposes.", | ||
@@ -27,4 +27,4 @@ "main": "index.js", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.0.0" | ||
"mocha": "^5.0.1" | ||
} | ||
} |
@@ -11,4 +11,4 @@ 'use strict'; | ||
describe('require', () => { | ||
it('require should cache and return cached version on a second call', (done) => { | ||
const datetimeA = require('./resources/datetime'); | ||
it('require should cache and return cached version on the second call', (done) => { | ||
const datetimeA = cleanrequire('./resources/datetime'); | ||
setTimeout(() => { | ||
@@ -23,10 +23,25 @@ const datetimeB = require('./resources/datetime'); | ||
describe('cleanrequire', () => { | ||
it('cleanrequire should return a NON CACHED version on a second call', (done) => { | ||
const datetimeA = require('./resources/datetime'); | ||
setTimeout(() => { | ||
const datetimeB = cleanrequire('./resources/datetime'); | ||
expect(datetimeA).to.not.equal(datetimeB); | ||
done(); | ||
}, 10); | ||
context('When loading a relative file', () => { | ||
it('cleanrequire should return a NON CACHED version on the second call', (done) => { | ||
const datetimeA = require('./resources/datetime'); | ||
setTimeout(() => { | ||
const datetimeB = cleanrequire('./resources/datetime'); | ||
expect(datetimeA).to.not.equal(datetimeB); | ||
done(); | ||
}, 10); | ||
}); | ||
}); | ||
context('When loading a library from node_modules', () => { | ||
it('cleanrequire should return a NON CACHED version on the second call', (done) => { | ||
const chaiA = require('istanbul'); | ||
setTimeout(() => { | ||
const chaiB = cleanrequire('istanbul'); | ||
expect(chaiA).to.deep.equal(chaiB); | ||
expect(chaiA).to.not.equal(chaiB); | ||
done(); | ||
}, 10); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5550
58