fix(mock-doc): add hasAttribute to MockCSSStyleDeclaration #2789
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.
Proposed changes:
hasAttribute
implementation toMockCSSStyleDeclaration
Without this I sometimes run into the following error with
jest@26
:The reason this happens is because the css style declaration is proxied, and the
ProxyHandler
's get method returns an empty string for unknown properties:stencil/src/mock-doc/css-style-declaration.ts
Line 74 in a1c320b
stencil/src/mock-doc/css-style-declaration.ts
Lines 14 to 17 in a1c320b
When jest tries to pretty-format, it checks whether the object has a
hasAttribute
property, and if it does, it assumes that it's a function and tries calling it (which fails because the proxy returns a string''
).My first assumption was that
MockCSSStyleDeclaration
is supposed to be the mock for the<style>
element, in which case I thought it might also make sense that it extendsMockHTMLElement
so that all element-related methods are implemented. But now I see that this is regarding thestyle
attribute, so maybe it makes sense to get this fixed inpretty-format
instead:https://github.com/facebook/jest/blob/7430a7824421c122cd07035d800d22e1007408fa/packages/pretty-format/src/plugins/DOMElement.ts#L30
(
pretty-format
is a bit optimistic here to assume thathasAttribute
is always callable like this)