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

Jest CSS enhancement suggestion: don't lose classNames in snapshot tests. #5622

Open
dawnmist opened this issue Oct 30, 2018 · 8 comments
Open

Comments

@dawnmist
Copy link

We've been using react-app-rewired with create-react-app v1 and react-scripts-ts for Typescript support for a while, and are starting to look at the new v2.1 release with Typescript support.

One of the enhancements we are using in jest snapshot tests is to output the classnames from css imports in the snapshot. This enables the ability to test parts of a component where a change in props or a user interaction is supposed to result in a change in css being applied to the component (e.g. as a simple make-believe example, changing className from "" to "locallyModified" where there is a :local(.locallyModified) { color: #FFFF44; } class in the css).

The snapshot then contains <div className="locallyModified"> instead of simply <div>, which highlights an issue when someone makes a change that breaks the behaviour identifying when the conditional styling should be altered.

The actual cssTransforms.js file is simply:

const idObj = new Proxy(
  {},
  {
    get: function getter(target, key) {
      return key;
    }
  }
);

module.exports = idObj;

Is it possible to consider adding this to react-scripts/config/jest/cssTransform.js by default? Or is there a reason why it couldn't/shouldn't be widely used?

@Timer
Copy link
Contributor

Timer commented Oct 30, 2018

/cc @ro-savage

@ro-savage
Copy link
Contributor

ro-savage commented Oct 30, 2018 via email

@stale
Copy link

stale bot commented Nov 29, 2018

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Nov 29, 2018
@dawnmist
Copy link
Author

Please don't auto-close this issue - the person who is intending to look at it is currently away from home without a computer.

@stale stale bot removed the stale label Nov 30, 2018
@stale
Copy link

stale bot commented Dec 30, 2018

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Dec 30, 2018
@dawnmist
Copy link
Author

dawnmist commented Jan 2, 2019

Any chance that this proposed improvement can be checked for whether it should be used? The issue has gone stale again without response.

@stale
Copy link

stale bot commented Jan 12, 2019

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

@stale stale bot closed this as completed Jan 12, 2019
@Timer Timer reopened this Jan 12, 2019
@Timer Timer added this to the 3.0 milestone Jan 12, 2019
@Timer Timer removed the stale label Jan 12, 2019
@dawnmist
Copy link
Author

dawnmist commented Feb 2, 2019

Looking at the changes in CRA v2 css modules handling, the proxy I've used in the past won't work directly (as the styles were previously output as named exports, rather than the equivalent to a default object with named fields).

However, the same can be achieved by following the instructions on using webpack with Jest:
Mocking CSS Modules
Which refers to identity-obj-proxy to provide the actual mock.

I've been migrating to using typescript-plugin-css-module for handling the typescript type definitions for css/scss modules (which generates css/scss class names as the equivalent to a default object with named fields), and can confirm that my original jest mock will not work with a default object-like type definition, but identity-obj-proxy as advised in the Jest documentation does work properly.

@iansu iansu added this to To do in v3 Mar 10, 2019
@iansu iansu assigned iansu and mrmckeb and unassigned iansu Mar 20, 2019
@iansu iansu removed this from the 3.0 milestone Mar 21, 2019
@iansu iansu removed this from To do in v3 Mar 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants