From 2cde5faad05156c94a7298e74b9220c5b0348b55 Mon Sep 17 00:00:00 2001 From: Danny Kirchmeier Date: Tue, 13 Dec 2022 11:59:51 -0600 Subject: [PATCH] Protect replace from modifying the prototype mock(Knex) is storing the original functions on a replacement object, but when being set lodash is storing it on the real object prototype. This change modifies the replacement storage to put the function on a renamed prototype object much like the constructor. This allows unmock(Knex) to properly restore all mocked functions. Fixes #126 --- src/util/transformer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/util/transformer.js b/src/util/transformer.js index 61c83e2..d01db4b 100644 --- a/src/util/transformer.js +++ b/src/util/transformer.js @@ -32,11 +32,12 @@ class Mocker { _replace(obj, spec, replaced, path) { let replacement = _.get(spec, path); - path = path.replace('._constructor.', '.constructor.'); + path = path.replace('._constructor.', '.constructor.').replace('._prototype.', '.prototype.'); const context = this.context(obj, path); const name = _.last(path.split('.')); - const replacedPath = path.replace('.constructor.', '._constructor.'); + const replacedPath = path.replace('.constructor.', '._constructor.').replace('.prototype.', '._prototype.'); + if (! _.get(replaced, path)) { _.set(replaced, replacedPath, _.get(obj, path));