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(mock-doc): add hasAttribute to MockCSSStyleDeclaration #2789

Closed
wants to merge 1 commit into from

Conversation

simonhaenisch
Copy link
Contributor

Proposed changes:

  • add hasAttribute implementation to MockCSSStyleDeclaration

Without this I sometimes run into the following error with jest@26:

PrettyFormatPluginError: _val$hasAttribute.call is not a functionTypeError: _val$hasAttribute.call is not a function

      at testNode (node_modules/jest-matcher-utils/node_modules/pretty-format/build/plugins/DOMElement.js:32:27)

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:

return cssStyle.getPropertyValue(prop);

getPropertyValue(prop: string) {
prop = jsCaseToCssCase(prop);
return String(this._styles.get(prop) || '');
}

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 extends MockHTMLElement so that all element-related methods are implemented. But now I see that this is regarding the style attribute, so maybe it makes sense to get this fixed in pretty-format instead:

https://github.com/facebook/jest/blob/7430a7824421c122cd07035d800d22e1007408fa/packages/pretty-format/src/plugins/DOMElement.ts#L30

- val.hasAttribute?.('is');
+ typeof val.hasAttribute === 'function' && val.hasAttribute('is');

(pretty-format is a bit optimistic here to assume that hasAttribute is always callable like this)

@simonhaenisch simonhaenisch changed the title fix(mock-dock): add hasAttribute to MockCSSStyleDeclaration fix(mock-doc): add hasAttribute to MockCSSStyleDeclaration Jan 6, 2021
@simonhaenisch
Copy link
Contributor Author

simonhaenisch commented Jan 6, 2021

Just a quick update, my PR for fixing this in Jest's pretty-format has been merged, so this should also soon be solved by using a newer jest version. I'll close this PR for now.

@simonhaenisch simonhaenisch deleted the fix-mock-css-style-declaration branch January 6, 2021 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant