7. Snapshot Testing Flashcards

1
Q

What is a snapshot?

A

JSON-based record of a component’s output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does snapshot testing work?

A

A snapshot taken previously is compared to its corresponding component’s actual output. If everything matches, the test passes; otherwise, it fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can be said about snapshots when it comes to source control?

A

Snapshots are committed along with other and tests to the application repo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do you need to import in a file in order to create a snapshot?

A

The component you’d like to create the snapshot of, and the renderer from ‘react-test-renderer’.

eg.
~~~
import renderer from ‘react-test-renderer’;
import { MyComponent} from ‘./MyComponent’;
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do you use to create a tree?

A

The .create method of the renderer.

eg. const tree = renderer.create();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a tree in the context of snapshot testing?

A

A representation of the HTML output of the component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a snapshot from a tree?

A

By using the toMatchSnapshot assertion method and the toJSON method of the tree— the first time toMatchSnapshot is called, it creates the snapshot.

eg. expect(tree.toJSON()).toMatchSnapshot();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you assert that a tree matches a snapshot?

A

By using the toMatchSnapshot assertion method and the toJSON method of the tree — subsequent calls (after the first) to toMatchSnapshot asserts whether it matches the existing snapshot.

eg. expect(tree.toJSON()).toMatchSnapshot();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s are the six potential steps of a snapshot testing process?

hint: Developer A vs Developer B

A
  1. Developer A authors a new component
  2. Snapshot of that component’s output is generated automatically
  3. Developer A commits changes, moves on
    - -
  4. Developer B makes a breaking change to a dependency of the new component
  5. A new snapshot is automatically generated
  6. Since the snapshots don’t match, the test suite will fail until updated.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the five advantages of snapshot testing?

A
  1. Fast and automatic
  2. Catches regressions humans may miss
  3. Works nicely with libraries that take in a state and output HTML components (React, Angular, Vue)
  4. Adds some protection against regression when no time is available for manually writing tests
  5. Requires little training or knowledge of testing to use
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the six disadvantages of snapshot testing?

A
  1. Easy to ignore and suppress
  2. Protects only against regression
  3. If a component is working incorrectly and then is fixed, a snapshot test will say it is now broken
  4. Adds extra files to an already crowded repo
  5. Sensitive to incidental changes
  6. A waste of resources if the component in question is certain to be modified in the near future
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you update snapshots?

A

By running the tests with an --update flag.

eg. jest --update

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When should we update snapshots?

A

Only when the developer has legitimately verified that the change to the broken component is not a regression but rather intended.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly