decache | :shipit: Delete Cached node_modules useful when you need to "un-require" during testing for a fresh | Caching library
kandi X-RAY | decache Summary
kandi X-RAY | decache Summary
Delete a module from node.js' require.cache so you can freshly require it again. In node.js when you require() a module, node stores a cached version of the module, so that all subsequent calls to require() do not have to reload the module from the filesystem. decache ( Delete Cache ) lets you delete modules from node.js require() cache this is useful when testing your modules/projects.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of decache
decache Key Features
decache Examples and Code Snippets
Community Discussions
Trending Discussions on decache
QUESTION
I'd like to be able to stub my middleware functions on a per-test basis. The problem, as articulated here, is that I can't just stub my middleware functions since node has cached the middleware function so I can't stub it since I create my app at the very beginning.
...ANSWER
Answered 2019-Nov-13 at 10:05The problem is not really related to Node caching modules - it's express who stores a reference to a middleware function when a server is initially created.
After isAdmin
method of require
d module is stubbed, it's the cached version who gets stubbed, so using a tool like proxyquire
would only allow you to require a fresh version of the module (without stubbed method) if you need it for some reason.
If what you're looking for is adjusting behavior of a particular middleware for already created express server, you'd need a way to alter middleware function's behavior by its reference. Hopefully, sinon stubs (and others too, e.g. ones jest provides) are capable of that. However, you still need to stub the module before creating an express server so it stores a reference to the stubbed function.
Sample implementation might look as follows:
QUESTION
When checking for a new file inside my ./Assetto1/results
folder (.json file) the code should read the new file (result = require("[...]")
) and log it into console. But I'm always getting the same error. Even trying for 8 hours now. The same thing happens, when using fs.readFile()
instead of require()
. I even tried an npm module called "decache" so that I could somehow "derequire" the file. My idea was that nodejs then expects the end of the JSON file. But nothing of this helps, as well.
Take a look at my code i wrote:
...ANSWER
Answered 2018-Sep-10 at 14:19How is the file created? You probably just have a race condition with the file being created (which triggers the fs.watch
event and reading of the file), then the JSON is written after a bit of processing, but it's already too late. Or the JSON is big and it takes time to write it all.
Two options for you:
the process that creates the JSON should work in another directory (but on the same filesystem). Once the file is fully written out, move it to the directory. This way, you have an atomic change, and you know that when you receive the event the file is complete.
An alternative is to write in the same directory, but rename once finished, and have a convention for what files are temporary or final (e.g. if the file is .xxxx.tmp ignore the event).
or whenever you receive an event for a file (
rename
orchange
):- cancel any previously created timer for that file
- create a new timer for that file
- when the timer expires, actually read the file
QUESTION
I try to set stub fakes for a express middleware function and it's not replacing over.
What I'm trying (how to reproduce)I'm trying to use sinon stubbing via callsFake
function, just as it's advised from their most updated docs.
Even though I'm requiring the module and replacing the function from the property at the export. I keep seeing the original function behavior acting.
I know that I should try to get the function stubbed before the middleware functions get setup, and that's when express app
is first imported.
This is the function I'm trying to stub, defined as a function and exported as a object too. It's defined in a script file with a path like api/middlewares/stripe/signature
.
ANSWER
Answered 2018-Aug-30 at 11:54As a rule of thumb, stubs should be set up per test, i.e. in beforeEach
or it
, not in before
. Here they don't seem to contain per-test logic but they can, and in this case they won't work as expected with before
. mocha-sinon
should preferably be used to integrate Mocha with Sinon sandbox, so no afterEach
is needed to restore stubs, this is done automatically.
Since verifySignature
is export property and not the export itself, signatureMiddleware
module may be left as is, but modules that use it should be de-cached and re-imported in tests where they are expected to use verifySignature
. If the behaviour should be same for entire test suite, this should be performed in beforeEach
as well. E.g. if these middlewares are used in app
module directly, it's:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install decache
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page