I have a few questions when it comes to unit testing using Jest in React Native.
I run my tests now with
npm test
, is this the only way to run it or are the tests also being executed when building out the project? If so, can I disable auto testing?If a test fails, will that prevent the app from building or executing?
Is there any difference in behaviour if a test fails or succeeds on iOS and Android?
Example
Below code just tests if AsyncStorage works as expected. If the following test fails - sure AsyncStorage will not work if I am using it within the app, but will it prevent the app from being used entirely?
beforeEach(() => {
AsyncStorage.clear();
});
it('Test - Read AsyncStorage', async () => {
await AsyncStorage.setItem('username', 'testUser')
let usernameValue = await AsyncStorage.getItem('username')
expect(usernameValue).toBe('testUser')
});
I am happy to get any further tips - I just can't find specific answers to the above so thanks in advance, and I haven't really been doing much unit testing so quite new to this.
CodePudding user response:
Your tests are not executed when you build your project. If you are using a CICD platform, it may run
npm
commands to build your project and then run the tests after the build has successfully completed. However you may need to configure this yourself as building and testing are done exclusively by default.A failing test will not prevent your app from building and executing. It is just an indication that some logic may not behave the way you want it. If you are using a CICD platform and your tests fail, typically
jestjs
emits an error code of -1 upon encountering a failure which will then cause your pipeline to fail.The whole point of
react-native
is to be platform agnostic - as much as possible. So a failingjestjs
test will have the same effect for all of the platforms.