Writing test cases
Once you have your components hooked up, you're ready to write some tests!
Basic test structure
Cavy provides describe
and it
functions to write and group your test
cases. The basic structure of your tests will be something like this:
// specs/AppSpec.js
export default function(spec) {
spec.describe('My feature', function() {
spec.it('works', async function() {
await spec.fillIn('Scene.TextInput', 'some string')
await spec.press('Scene.button');
await spec.exists('NextScene');
});
});
}
Reference your hooked-up components using the string you provided to
generateTestHook
.
Test helpers
Cavy provides a set of functions you can use when writing your tests to interact with your components and manipulate your app.
See the Test Helper API reference for a comprehensive list of functions available.
Can't find something you need? Write your own test helper function! See Guides - Writing your own spec helpers for an example of writing your own assertions.
Test execution
You can use beforeEach
to call a function before each test runs. The
beforeEach
function will be called after AsyncStorage
is cleared but before
the app re-renders and the test is run.
The order of actions for each test execution is:
- AsyncStorage is cleared (if the
clearAsyncStorage
prop is set to true inTester
) - The
beforeEach
function is called - The app is re-rendered
- The test is run
// specs/AppSpec.js
export default function(spec) {
spec.beforeEach(function() {
// This will run before each test in this spec file.
});
spec.describe('My feature', function() {
spec.it('works', async function() {
await spec.fillIn('Scene.TextInput', 'some string')
await spec.press('Scene.button');
await spec.exists('NextScene');
});
});
}