0

This is regarding detox e2e tests. I am running my tests, each under an it('xx', async => { await...});

The tests are scripted in such a way that 1st test would log in, 2nd test would do something on homepage, 3 rd test would navigate from homepage to other pages and so on.

The issue here is, as soon as my first test executes, the app is getting logged out and all the consecutive tests are failing. But when I include all steps(right from login to the desired functionality) in every test, the suite is working properly.

I would like to know why is this happening. Is there any connection with async function?

Siva
  • 113
  • 1
  • 16
  • are you calling `await device.reloadReactNative();` at any point? can you share the code of one of your `*.spec.js` files? – Andrew Jan 30 '19 at 11:46
  • ah! Bingo. Exactly. In one of my files there is a beforeEach function. There I have added the reloadReactNative. Totally missed it. Thanks. – Siva Jan 30 '19 at 13:31

1 Answers1

3

One of the gotchas for using Detox is that the sample test specification uses a beforeEach and there is a tendency to copy verbatim examples that we are given, some times missing out things that either need to be removed or should be added.

In this particular case in the beforeEach there is the call await device.reloadReactNative(); this command reloads the device as if you had pressed CMD+R (on iOS) or RR (on Android). This means that items that have been saved to state are lost and the application is pretty much returned to its initial state.

Check your code for the offending line, you can see it in example provided below. If you remove this line then it will stop reloading React Native on your device before each test.

example.spec.js

https://github.com/wix/Detox/blob/master/examples/demo-react-native/e2e/example.spec.js

describe('Example', () => {
  beforeEach(async () => {
    await device.reloadReactNative(); // <- this is the problem
  });
  
  it('should have welcome screen', async () => {
    await expect(element(by.id('welcome'))).toBeVisible();
  });
  
  it('should show hello screen after tap', async () => {
    await element(by.id('hello_button')).tap();
    await expect(element(by.text('Hello!!!'))).toBeVisible();
  });
  
  it('should show world screen after tap', async () => {
    await element(by.id('world_button')).tap();
    await expect(element(by.text('World!!!'))).toBeVisible();
  });
});
Community
  • 1
  • 1
Andrew
  • 26,706
  • 9
  • 85
  • 101