From f5a958ab779e26ea11a30d8daa88772e241ab247 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Thu, 24 Aug 2023 17:51:59 -0400 Subject: [PATCH] fix(tests): change how `Proxy` is used for octokit rest It seems that rest octokit is alredy using some kind of meta-programming with conflicts with the way of how metrics mocks data (assignement does not override value, despite `writable: true, configurable: true` as property descriptors, and ends up hitting the real api). current fix will only support 2 level depth, but should be good enough for now --- tests/mocks/index.mjs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/mocks/index.mjs b/tests/mocks/index.mjs index 67fc1bb3..27c68db6 100644 --- a/tests/mocks/index.mjs +++ b/tests/mocks/index.mjs @@ -64,21 +64,27 @@ export default async function({graphql, rest}) { { //Unmocked console.debug("metrics/compute/mocks > mocking rest api") - const unmocked = {} + const unmocked = rest + //Mocked - const mocker = ({path = "rest", mocks, mocked}) => { - for (const [key, value] of Object.entries(mocks)) { - console.debug(`metrics/compute/mocks > mocking rest api > mocking ${path}.${key}`) - if (typeof value === "function") { - unmocked[path] = Object.assign(value, mocked[key]) - mocked[key] = new Proxy(unmocked[path], {apply: value.bind(null, {faker})}) - } - else { - mocker({path: `${path}.${key}`, mocks: mocks[key], mocked: mocked[key]}) + rest = new Proxy(unmocked, { + get(target, section) { + if (Reflect.has(mocks.github.rest, section)) { + return new Proxy(target[section], { + get(target, property) { + if (mocks.github.rest?.[section]?.[property]) { + return async function () { + console.debug(`metrics/mocking > rest.${section}.${property}`) + return mocks.github.rest[section][property]({faker}, target, null, arguments) + } + } + return Reflect.get(target, property) + } + }) } + return Reflect.get(target, section) } - } - mocker({mocks: mocks.github.rest, mocked: rest}) + }) } //Axios mocking