mfncooper / mockery

Simplifying the use of mocks with Node.js
Other
1.1k stars 60 forks source link

Selective clear when useCleanCache is true #49

Open alanszp opened 8 years ago

alanszp commented 8 years ago

There is a missing feature, that when I use useCleanCache: true, I can't specify a file/folder to be excluded from the cleaning, and to remain as a chached module.

The problems is this: I have a file that creates an in-memory database with bookshelf.js (and a few options setting the connection is to a sqlite db). If I use useCleanCache: true, that file will be "unloaded" and when I require it again in my tests, it will be loaded again. The result of this, will be that I will have an unlodaded in-memory database and a new in-memory database.

Notes: I know there is a discussion if I should or shouldn't test with a db or not. I know it's not the best practice to do it, because it won't be a real unit test, but I would like to in this case. Also, setting up a real db for testing would make the testing slower. I don't want to enter in this discusion right now :D

The solution: Add a new option: remainingCache: [RegExp] which will contain an array of RegExps, and will be used only if useCleanCache: true. This will change the override of m._cache = {} with an object filled with the original modules that the file name matched any of the RegExps.

I will make a PR with this if you are ok!

javieranselmi commented 6 years ago

I am having exactly the same issue!