Search code examples
ember.jsember-simple-auth

Ember simple auth tests failing intermittently


I am struggling to debug failing tests on an Ember 3.0 app. I have a login form using ember-simple-auth. The authenticate action looks like this:

let { identification, password } = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:devise', identification, password)
.catch((reason) => {
    this.set('loginError', reason.errors || reason);
});

My test simply fills in the form and clicks a button to trigger this action, then checks a user is logged in:

invalidateSession();
await visit('/');

fillIn('#identification', '[email protected]');
fillIn('#password', 'secret');
await click('.login-btn');

let sesh = currentSession();
let isAuthed = get(sesh, 'isAuthenticated');
assert.equal(
  isAuthed,
  true,
  'after a user submits good creds to login form, they are logged in'
);

Using a Mirage backend, this works every time. However, using a Rails API which returns the same response, this test fails about half the time. It seems to be random whether it will pass or fail.

I'm not sure where the problem is here. The Rails app is running locally on port 3000 so it is not a network issue. I wonder if the test is timing out because the API takes longer to respond than Mirage - although this does seem unlikely as the tests run in under a second. Has anyone encountered a problem like this?

Thanks


Solution

  • My hunch is that you may be evaluating isAuth before ember-simple-auth has completed authenticating with the backend. For my tests that are similar to yours, I have await settled(); after await click();.

    If that doesn't do the trick then you could try using waitFor or waitUntil from the ember-test-helpers to make sure that authentication has finished.