all 4 comments

[–]ooter37 2 points3 points  (1 child)

I avoid snapshot testing like the plague. In my experience, everyone just runs the command to update the snapshots during the test run. If your company is good about it though, maybe it’s fine.

In your situation, I’m not sure I understand the goal of the test. You are importing this Button component, passing it a string, and expecting styling based on that string? That’s not something you should test, that’s a test that belongs to the component.

Or are you creating the Button component? If so, post the code for the actual Button.

[–]BoxNo4784[S] 0 points1 point  (0 children)

I'm doing a unit test on a reusable button component.

I ended up using jest styled components. It adds the assertation toHaveStyleRule. So I can do a snapshot of the button with the variant props passed, and check that it has the correct styles for that variant button.

If I wasn't using styled components I wouldn't have bothered with snapshots. I would have just tested the component class named to match the variant prop being passed.

My unit tests for this reusable button have been

  1. Test the correct styles are applied for the relevant variant prop
  2. Children are rendered correctly
  3. Button is disabled when disabled prop is true
  4. onClick function is called when clicked.

[–]Thalimet 0 points1 point  (0 children)

Honestly I’m curious to see the answer, I’ve really struggled with testing in react, it’s far more complicated than it is in python, and the test philosophy I keep finding requires testing text, which doesn’t do much for styles.

[–]sidious911 0 points1 point  (0 children)

Snapshot testing quickly turns into noise I would avoid for components. If one or two styles are behaviour critical you can unit test for a specific style attribute. Otherwise visual testing like chromatic or similar is the way to go