fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

Hashed classNames mismatch on snapshot tests

Open jandramila opened this issue 8 years ago • 9 comments

Bug Report

  • Package version(s): 5.98.0

Priorities and help requested:

Are you willing to submit a PR to fix? No

Requested priority: Blocking

Describe the issue:

Fabric UI React components seem to give different hashed classNames to underlying HTML when changing from one environment to another.

Actual behavior:

I'm getting different classNames for Fabric UI Components like TextField when running snapshot tests on different machines.

For example:

Original snapshot output, commited and pushed from my Mac Mini.

className="ms-TextField-field field_f5689719"

Coworker's Macbook Pro snapshot test output, differs from the one above.

className="ms-TextField-field field_78c08576"

Expected behavior:

Don't really know, is this package supposed to support snapshot testing?

jandramila avatar Jun 13 '18 20:06 jandramila

Are you using Jest for snapshotting? We've created a utility to convert those hashed classes into the actual styles passed into them.

https://www.npmjs.com/package/@uifabric/jest-serializer-merge-styles

micahgodbolt avatar Jun 19 '18 17:06 micahgodbolt

Excellent!! Thank you very much @micahgodbolt , that did the work.

jandramila avatar Jun 26 '18 13:06 jandramila

Sorry @micahgodbolt, I'm reopening as it still seems that I'm having dynamically generated classes inside my snapshots, although some of the styling does gets merged into the snapshot:

My package.json config looks like this:

"jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/config/jest/enzymeAdapter.js",
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
    ],
    "testEnvironment": "node",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs"
    ],
    "snapshotSerializers": [
      "@uifabric/jest-serializer-merge-styles"
    ]
  },

I was using enzyme to render snapshots but got back to react-test-renderer.


I'm getting this snapshot:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`standalone DropdownInput component matches the snapshot 1`] = `
<div
  className="ms-Dropdown-container"
>
  <div
    aria-autocomplete="none"
    aria-describedby="Dropdown0-option"
    aria-expanded="false"
    aria-live="assertive"
    className="ms-Dropdown root_0df10b7b"
    data-is-focusable={true}
    id="Dropdown0"
    onBlur={[Function]}
    onClick={[Function]}
    onKeyDown={[Function]}
    onKeyUp={[Function]}
    role="listbox"
    tabIndex={0}
  >
    <span
      aria-atomic={true}
      aria-readonly="true"
      className="ms-Dropdown-title title_0df10b7b"
      id="Dropdown0-option"
      role="listbox"
    >
      <span>
        Test value 1
      </span>
    </span>
    <span
      className="ms-Dropdown-caretDownWrapper caretDownWrapper_0df10b7b"
    >
      <i
        aria-hidden={true}
        className=
            ms-Dropdown-caretDown
            caretDown_0df10b7b
            {
              -moz-osx-font-smoothing: grayscale;
              -webkit-font-smoothing: antialiased;
              display: inline-block;
              font-family: "FabricMDL2Icons";
              font-style: normal;
              font-weight: normal;
              speak: none;
            }
        data-icon-name="ChevronDown"
        role="presentation"
      >
        
      </i>
    </span>
  </div>
</div>
`;

Which seems weird because some styles are getting merged into snapshots but there are still some auto generated classnames remaining, that obviously break the whole snapshot test.

jandramila avatar Jun 26 '18 19:06 jandramila

The reason is because the caretDown_0df10b7b class name is coming from sass generated css module names. We are moving away from sass, and for components that use merge-styles, the above mentioned serializer works awesome. But for the remaining components that are still currently being converted (we're almost done!), you can work around this:

In your jest config, map scss files to a style mock file:

        moduleNameMapper: {
          '\\.(scss)$': path.resolve(__dirname, 'jest-style-mock.js'),
        }

The jest-style-mock.js file just has this:

module.exports = {};

This makes all the sass generated class names blank, which should alleviate your issue. Sorry for the confusion. Like I mentioned, we're working on wrapping up the last few components within the next couple weeks and by then this workaround will not be required. :)

dzearing avatar Jul 02 '18 15:07 dzearing

What is the work around for people who use CRA? moduleNameMapper unfortunately isn't supported. Also, this issue is almost from a year ago and we're running into this. What is the status of moving away from Sass? Let me know if you need more information, this is a diff of what it looks like anytime someone touches a component image Maybe this is a different issue?

elleaura avatar Jun 17 '19 15:06 elleaura

What version of Fabric are you using? We've almost completed removed Sass now.

micahgodbolt avatar Jun 17 '19 16:06 micahgodbolt

^6.190.0

elleaura avatar Jun 17 '19 17:06 elleaura

@lalicari It seems like you're not using the jest serializer for the class names.

Did you try this?

https://www.npmjs.com/package/@uifabric/jest-serializer-merge-styles

dzearing avatar Jun 18 '19 04:06 dzearing

Ah, that's it, thanks!

elleaura avatar Jun 24 '19 19:06 elleaura