Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat: add mock proxy object
Browse files Browse the repository at this point in the history
  • Loading branch information
pevisscher committed Nov 29, 2022
1 parent 4ba5aed commit cfbbf18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mock'
1 change: 1 addition & 0 deletions src/lib/test/mock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mock } from './mock'
17 changes: 17 additions & 0 deletions src/lib/test/mock/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function mock<T>(): jest.MaybeMocked<T> & { mockClear(): void } {
const cache = new Map<any, jest.Mock>()
const handler: ProxyHandler<object> = {
get: (_, name) => {
if (name === 'mockClear') {
return () => cache.clear()
}

if (!cache.has(name)) {
cache.set(name, jest.fn().mockName(name.toString()))
}

return cache.get(name)
},
}
return new Proxy({}, handler) as jest.MaybeMocked<T> & { mockClear(): void }
}

0 comments on commit cfbbf18

Please sign in to comment.