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
This commit is contained in:
Simon Lecoq
2023-08-24 17:51:59 -04:00
parent f06c102e6a
commit f5a958ab77

View File

@@ -64,21 +64,27 @@ export default async function({graphql, rest}) {
{ {
//Unmocked //Unmocked
console.debug("metrics/compute/mocks > mocking rest api") console.debug("metrics/compute/mocks > mocking rest api")
const unmocked = {} const unmocked = rest
//Mocked //Mocked
const mocker = ({path = "rest", mocks, mocked}) => { rest = new Proxy(unmocked, {
for (const [key, value] of Object.entries(mocks)) { get(target, section) {
console.debug(`metrics/compute/mocks > mocking rest api > mocking ${path}.${key}`) if (Reflect.has(mocks.github.rest, section)) {
if (typeof value === "function") { return new Proxy(target[section], {
unmocked[path] = Object.assign(value, mocked[key]) get(target, property) {
mocked[key] = new Proxy(unmocked[path], {apply: value.bind(null, {faker})}) if (mocks.github.rest?.[section]?.[property]) {
} return async function () {
else { console.debug(`metrics/mocking > rest.${section}.${property}`)
mocker({path: `${path}.${key}`, mocks: mocks[key], mocked: mocked[key]}) 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 //Axios mocking