Skip to content

Commit

Permalink
feat: improve internal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 23, 2023
1 parent a34392e commit 44db232
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './spy'
export * from './spyOn'
export * from './restoreAll'
export { createInternalSpy, spies, getInternalState } from './internal'
export type { Spy, SpyImpl } from './internal'
export type { Spy, SpyImpl, SpyInternal, SpyInternalImpl } from './internal'
9 changes: 7 additions & 2 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { assert, define, defineValue, isPromise, isType } from './utils'
import { S } from './constants'

interface GetState {
<A extends any[], R>(spy: SpyInternalImpl<A, R>): SpyInternalImplState<A, R>
<A extends any[], R>(spy: SpyInternal<A, R>): SpyInternalState<A, R>
}

export let spies = new Set<SpyImpl>()

let reset = (state: SpyInternalState) => {
Expand All @@ -13,8 +18,8 @@ let defineState = (spy: SpyInternal) => {
define(spy, S, { value: { reset: () => reset(spy[S]) } })
return spy[S]
}
export let getInternalState = <A extends any[], R>(spy: SpyInternal<A, R>) => {
return spy[S] || defineState(spy)
export let getInternalState: GetState = (spy) => {
return (spy[S] || defineState(spy)) as any
}

type ReturnError = ['error', any]
Expand Down
2 changes: 1 addition & 1 deletion src/spyOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Constructors<T> = {
let getDescriptor = (obj: any, method: string | symbol | number) =>
Object.getOwnPropertyDescriptor(obj, method)

export function internalSpyOn<T extends object, K extends string & keyof T>(
export function internalSpyOn<T, K extends string & keyof T>(
obj: T,
methodName: K | { getter: K } | { setter: K },
mock?: Procedure
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function assert(condition: any, message: string) {
export function assert(condition: any, message: string): asserts condition {
if (!condition) {
throw new Error(message)
}
Expand Down

0 comments on commit 44db232

Please sign in to comment.