From c86281e7e414729586a733e1cfa154936f5f0c65 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 15 Oct 2015 00:20:20 +0200 Subject: [PATCH] Also reapply the test framework patch when a parent module of index.js is retrieved from require's cache. Fixes the scenario described in #205 when there's an unexpected-with-plugins.js or similar construct involved. --- lib/index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index ff65036e7..01980acba 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,16 +1,23 @@ -/*global __filename*/ var testFrameworkPatch = require('./testFrameworkPatch'); -if (testFrameworkPatch.applyPatch() && typeof require === 'function' && require.cache) { - // Make sure that the 'it' global gets patched in every context where Unexpected is required, - // but prevent rereading index.js from disc each time: - Object.defineProperty(require.cache, __filename, { + +function applyTestFrameworkPatchWhenLoaded(mod) { + Object.defineProperty(require.cache, mod.id, { get: function () { testFrameworkPatch.applyPatch(); - return module; + return mod; } }); } +if (testFrameworkPatch.applyPatch() && typeof require === 'function' && require.cache) { + // Make sure that the 'it' global gets patched in every context where Unexpected is required, + // but prevent rereading index.js from disc each time: + + for (var mod = module ; mod ; mod = mod.parent) { + applyTestFrameworkPatchWhenLoaded(mod); + } +} + module.exports = require('./Unexpected').create() .use(require('./styles')) .use(require('./types'))