half-halt / svg-jest

A small utility for Jest for converting SVG files into react components for testing.
MIT License
15 stars 5 forks source link

svg-jest

npm GitHub issues

This is a small library which transforms .SVG files for jest. It produces and SVG in the stream.

The transformed item will have the following properties on it.

Works with both of these formats:

import MySvg from '../images/an-image.svg';

import { ReactComponent as MySvg}  from '../images/an-image.svg';

The following JavaScript

const MyComponent = () => {
  return (
    <div>
    <MySvg/>
    </div>
  );
}

The resulting snapshot:

<div>
    <AnImage/>
</div>

The resulting HTML:

<div>
 <svg 
    data-jest-file-name='an-image.svg' 
    data-jest-svg-name='an-image' 
    data-testid='an-image'/>
</div>

In additoin, any properties passed to '' are passed along into both the snapshot and the resulting trees.

Usage

Configuring Jest, the example below uses package.json, but you can see: https://jestjs.io/docs/en/configuration for other examples.

{
    "jest": {
        "transform": {
            "\\.svg$": "svg-jest"
        }
    }
}