fix(jest-mock): remove leftover own accessor when restoring spyOn of an inherited getter/setter#16226
Open
LeSingh1 wants to merge 1 commit into
Open
fix(jest-mock): remove leftover own accessor when restoring spyOn of an inherited getter/setter#16226LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
…herited getter/setter
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
jest.spyOn(object, key, 'get' | 'set')supports spying on an accessor that lives on the prototype chain rather than on the object itself. When the accessor is inherited, the spy is installed by defining a new own property on the object viaObject.defineProperty(object, propertyKey, descriptor). The restore step then re-defines that same own descriptor on the object instead of removing it.The result is that after
mockRestore, the object keeps an own accessor descriptor that shadows the prototype. The value read right after restore is still correct because the restored descriptor copied the prototype accessor, but the link to the prototype is gone, so later changes to the prototype accessor are no longer observed through the instance, andhasOwnProperty(key)staystrueon something that was originally inherited.The method branch of
spyOnalready handles this correctly: it tracks whether the method was an own property and deletes the own property on restore when it was inherited (see the existingisMethodOwnerhandling and the "should delete previously inexistent methods when restoring" test). This change applies the same approach to the accessor branch in_spyOnProperty: when the spied accessor was inherited, restoring now deletes the own property so the original prototype accessor is exposed again.Test plan
Added two tests under
packages/jest-mock/src/__tests__/index.test.tsin thespyOnPropertyblock, covering an inherited getter and an inherited setter. Each test spies on the inherited accessor, restores, and asserts that no own descriptor is left on the instance and that the prototype accessor is still in effect (including after the prototype accessor is later replaced).Both tests fail on
mainand pass with this change. The fulljest-mockunit suite passes.