@samitouri / QOS-React-2 / commits / 2d61639f90
Fork 0

The library for web and native user interfaces.

Store dirtyMocks array globally so it can survive dumpCache().

When require("mock-modules").dumpCache() is called, all mock functions previously created continue to refer to the old dirtyMocks array. If we replace that array with a new one, those mock functions will never have their .mockClear() methods called again. The upstream version of mocks.js pulls a similar global trick, and I never understood why until now.

Ben Newman committed Jul 8, 2013 at 19:19 UTC 2d61639f9089e58bf33b29264c6a09fdd1c91637
1 file changed +5 -4
src/test/mocks.js
+5 -4
@@ -72,7 +72,7 @@ function makeComponent(metadata) {
72 metadata.members.prototype.members) || {};
73
74 var f = function() {
75 - dirtyMocks.push(f);
75 + global.dirtyMocks.push(f);
76
77 instances.push(this);
78 calls.push(Array.prototype.slice.call(arguments));
@@ -305,7 +305,8 @@ function removeUnusedRefs(metadata) {
305 });
306 }
307
308 -var dirtyMocks = [];
308 +var global = Function("return this")();
309 +global.dirtyMocks = global.dirtyMocks || [];
310
311 module.exports = {
312 /**
@@ -313,8 +314,8 @@ module.exports = {
314 * called since the last time clear was called.
315 */
316 clear: function() {
316 - var old = dirtyMocks;
317 - dirtyMocks = [];
317 + var old = global.dirtyMocks;
318 + global.dirtyMocks = [];
319 old.forEach(function(mock) {
320 mock.mockClear();
321 });