How it works...

Since all our snapshot tests' filenames end with .snapshot.js, we can run all the snapshot tests with a single command:

npm test snapshot

The first time you run the tests, as before, the snapshots will be created: 

As with React, the first run will create snapshots for components

If we check the __snapshots__ directory, we will find the three produced .snap files within. Their format is the same as with the React examples that we developed earlier. Let's just have a look at the <RegionsTable> one, which we showed earlier:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`RegionsTable renders correctly a list 1`] = `
<RCTScrollView
style={
Array [
undefined,
Object {
"backgroundColor": "lightgray",
},
]
}
>
<View>
<View>
<Text
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
>
Cerro Largo
</Text>
</View>
<View>
<Text
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
>
Maldonado
</Text>
</View>
<View>
<Text
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
>
Montevideo
</Text>
</View>
</View>
</RCTScrollView>
`;

exports[`RegionsTable renders correctly an empty list 1`] = `
<View
style={undefined}
>
<Text
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
>
No regions.
</Text>
</View>
`;

If in the future you run the tests again and nothing has been changed, then the results will be three PASS green messages:

Our snapshot tests were all successful

Everything is working fine, so we can aver that writing snapshot tests doesn't add any complications to RN testing, and can be carried out without difficulty.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset