Challenge - 5 Problems
Snapshot Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What does this snapshot test verify?
Consider this React Native snapshot test code:
What is this test checking?
import React from 'react';
import renderer from 'react-test-renderer';
import Button from './Button';
test('Button renders correctly', () => {
const tree = renderer.create(<Button title='Click me' />).toJSON();
expect(tree).toMatchSnapshot();
});What is this test checking?
React Native
import React from 'react'; import renderer from 'react-test-renderer'; import Button from './Button'; test('Button renders correctly', () => { const tree = renderer.create(<Button title='Click me' />).toJSON(); expect(tree).toMatchSnapshot(); });
Attempts:
2 left
💡 Hint
Snapshot tests compare the rendered output structure to a saved version.
✗ Incorrect
Snapshot testing captures the rendered UI tree and compares it to a saved snapshot file. This test ensures the Button's UI structure has not changed unexpectedly.
❓ lifecycle
intermediate1:30remaining
When should you update a snapshot test?
You run a snapshot test and it fails because the UI changed intentionally. What should you do next?
Attempts:
2 left
💡 Hint
Snapshots should match the intended UI state.
✗ Incorrect
When UI changes are intentional, update the snapshot so future tests compare against the new correct UI.
🔧 Debug
advanced2:00remaining
Why does this snapshot test fail with a syntax error?
Look at this snapshot test snippet:
Why might this cause a syntax error?
test('renders text', () => {
const tree = renderer.create(Hello ).toJSON()
expect(tree).toMatchSnapshot()
})Why might this cause a syntax error?
Attempts:
2 left
💡 Hint
JSX elements must be imported or defined before use.
✗ Incorrect
If is not imported from 'react-native' or defined, the JSX parser throws a syntax error.
🧠 Conceptual
advanced1:30remaining
What is a limitation of snapshot testing in React Native?
Which of these is a common limitation of snapshot tests?
Attempts:
2 left
💡 Hint
Snapshot tests compare UI output, not how components behave.
✗ Incorrect
Snapshot tests capture UI structure but do not test how components behave or respond to events.
📝 Syntax
expert2:30remaining
What is the output of this snapshot test code?
Given this test code:
What will be printed to the console when this test runs?
import React from 'react';
import renderer from 'react-test-renderer';
const MyComponent = () => <Text>Hello</Text>
test('MyComponent snapshot', () => {
const tree = renderer.create(<MyComponent />).toJSON();
console.log(tree.type);
expect(tree.type).toBe('Text');
});What will be printed to the console when this test runs?
Attempts:
2 left
💡 Hint
What does toJSON() return for a React Native component without importing Text?
✗ Incorrect
Without importing Text from 'react-native', the JSX is treated as a custom component, so the rendered JSON's type is undefined.