Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Merge requests
  • !6706

Generate SVG component name in Jest fileTransform

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/dallonf/bugfix/stable-svg-snapshot into master Mar 21, 2019
  • Overview 6
  • Commits 2
  • Pipelines 0
  • Changes 2

Created by: dallonf

I noticed that in Jest, an SVG's ReactComponent (https://facebook.github.io/create-react-app/docs/adding-images-fonts-and-files#adding-svgs) doesn't have a displayName and it will appear as just <ForwardRef>, which could be a little confusing. This is particularly noticeable in snapshot tests, particularly with Enzyme, which includes both React components and HTML elements in the resulting snapshot.

This PR adds a function name generated the same way that SVGO generates component names (so it will be very close, probably the same, to the component's displayName in a production build).

Note that this adds a dependency to camelcase, but this is already a transitive dependency in the project (several different versions of it, in fact!), so it shouldn't actually result in any new dependencies installed.

How I Tested This

I set up enzyme and jest-enzyme in the template, and wrote this test:

import React from 'react';
import { mount, configure } from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';
import { ReactComponent as SvgLogo } from './logo.svg';

configure({ adapter: new EnzymeAdapter() });

it('renders SVG snapshot', () => {
  const wrapper = mount(<SvgLogo />);
  expect(wrapper).toMatchSnapshot();
});

Which produces a snapshot that looks like this:

BEFORE:

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

exports[`renders SVG snapshot 1`] = `
<ForwardRef>
  <svg>
    logo.svg
  </svg>
</ForwardRef>
`;

AFTER:

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

exports[`renders SVG snapshot 1`] = `
<ForwardRef(SvgLogo)>
  <svg>
    logo.svg
  </svg>
</ForwardRef(SvgLogo)>
`;
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/dallonf/bugfix/stable-svg-snapshot