Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inherit original prototype when spying #42

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
matrix:
node-version:
- 16
- 14
- 18
name: Node.js ${{ matrix.node-version }} Quick
steps:
- name: Checkout the repository
Expand All @@ -17,9 +17,11 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install pnpm
uses: pnpm/action-setup@v2
- name: Install dependencies
run: yarn install --frozen-lockfile --ignore-engines --ignore-scripts
run: pnpm install --frozen-lockfile
- name: Run unit tests
run: yarn test
run: pnpm test
env:
FORCE_COLOR: 2
35 changes: 27 additions & 8 deletions src/spyOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type Constructors<T> = {
let getDescriptor = (obj: any, method: string | symbol | number) =>
Object.getOwnPropertyDescriptor(obj, method)

let prototype = (fn: any, val: any) => {
if (val != null && typeof val === 'function' && val.prototype != null) {
// inherit prototype, keep original prototype chain
Object.setPrototypeOf(fn.prototype, val.prototype)
}
}

export function internalSpyOn<T, K extends string & keyof T>(
obj: T,
methodName: K | { getter: K } | { setter: K },
Expand All @@ -38,7 +45,10 @@ export function internalSpyOn<T, K extends string & keyof T>(
'cannot spyOn on a primitive value'
)

let getMeta = (): [string | symbol | number, 'value' | 'get' | 'set'] => {
let [accessName, accessType] = ((): [
string | symbol | number,
'value' | 'get' | 'set'
] => {
if (!isType('object', methodName)) {
return [methodName, 'value']
}
Expand All @@ -52,9 +62,7 @@ export function internalSpyOn<T, K extends string & keyof T>(
return [methodName.setter, 'set']
}
throw new Error('specify getter or setter to spy on')
}

let [accessName, accessType] = getMeta()
})()
let objDescriptor = getDescriptor(obj, accessName)
let proto = Object.getPrototypeOf(obj)
let protoDescriptor = proto && getDescriptor(proto, accessName)
Expand Down Expand Up @@ -92,6 +100,9 @@ export function internalSpyOn<T, K extends string & keyof T>(
if (!mock) mock = origin

let fn = createInternalSpy(mock)
if (accessType === 'value') {
prototype(fn, origin)
}
let reassign = (cb: any) => {
let { value, ...desc } = originalDescriptor || {
configurable: true,
Expand All @@ -103,9 +114,10 @@ export function internalSpyOn<T, K extends string & keyof T>(
;(desc as PropertyDescriptor)[accessType] = cb
define(obj, accessName, desc)
}
let restore = () => originalDescriptor
? define(obj, accessName, originalDescriptor)
: reassign(origin)
let restore = () =>
originalDescriptor
? define(obj, accessName, originalDescriptor)
: reassign(origin)
const state = fn[S]
defineValue(state, 'restore', restore)
defineValue(state, 'getOriginal', () => (ssr ? origin() : origin))
Expand All @@ -114,7 +126,14 @@ export function internalSpyOn<T, K extends string & keyof T>(
return fn
})

reassign(ssr ? () => fn : fn)
reassign(
ssr
? () => {
prototype(fn, mock)
return fn
}
: fn
)

spies.add(fn as any)
return fn as any
Expand Down
36 changes: 35 additions & 1 deletion test/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,49 @@ describe('class mock', () => {
expect(spy2.callCount).toBe(1)
})

test('spy keeps instance', () => {
test('spy keeps instance on a function', () => {
function Test() {}
const method = spy()
Test.prototype.run = method
const obj = {
Test,
}
const fn = spyOn(obj, 'Test')
const instance = new obj.Test()
expect(fn.called).toBe(true)
expect(instance).toBeInstanceOf(obj.Test)
expect(instance.run).toBe(method)
})

test('spy keeps instance on a function getter', () => {
function Test() {}
const method = spy()
Test.prototype.run = method
const obj = {
get Test() {
return Test
},
}
const fn = spyOn(obj, 'Test')
const instance = new obj.Test()
expect(fn.called).toBe(true)
expect(instance).toBeInstanceOf(obj.Test)
expect(instance.run).toBe(method)
})

test('spy keeps instance on a class', () => {
const method = spy()
class Test {
run = method
}
const obj = {
Test,
}
const fn = spyOn(obj, 'Test')
const instance = new obj.Test()
expect(fn.called).toBe(true)
expect(instance).toBeInstanceOf(obj.Test)
expect(instance.run).toBe(method)
})

describe('spying on constructor', () => {
Expand Down
Loading