jest-styled-components icon indicating copy to clipboard operation
jest-styled-components copied to clipboard

Compability with snapshot-diff

Open microcipcip opened this issue 7 years ago • 2 comments

Is this library supposed to work with snapshot-diff? I see different class output when using it like this:

import 'jest-styled-components'
import { render } from 'react-testing-library'
import snapshotDiff from 'snapshot-diff'
import Component from '../Component'

it('', async () => {
  const { container: containerWithIntro } = render(
    <Component intro />
  )
  const { container } = render(
    <Component />
  )
  expect(snapshotDiff(containerWithIntro, container)).toMatchSnapshot()
})

microcipcip avatar Jan 08 '19 02:01 microcipcip

ping

// setupTests.ts
import { toMatchDiffSnapshot } from 'snapshot-diff';
import 'jest-styled-components';

expect.extend({ toMatchDiffSnapshot });

alextrastero avatar Nov 14 '19 11:11 alextrastero

Not sure if it's on time but here what you can do to achive what you need:

import { styleSheetSerializer } from 'jest-styled-components';
import { diff } from 'jest-diff';

snapshot.addSerializer(styleSheetSerializer);

const identity = (value: any) => value;
expect.extend({
  toMatchDiffSnapshot(valueA, valueB) {
    const stringA = snapshot.utils.serialize(valueA);
    const stringB = snapshot.utils.serialize(valueB);

    const result = diff(stringA, stringB, {
      aColor: identity,
      bColor: identity,
      changeColor: identity,
      commonColor: identity,
      patchColor: identity,
      expand: false,
      includeChangeCounts: true,
      aAnnotation: 'First value',
      bAnnotation: 'Second value',
    });

    return snapshot.toMatchSnapshot.call(this as any, `Snapshot Diff:\n${result}`, '');
  },
});

AtnesNess avatar Apr 21 '22 01:04 AtnesNess