ember-cli / ember-cli-mocha

Mocha and Chai tests for ember-cli applications
Apache License 2.0
147 stars 54 forks source link

Async Integration not working #85

Closed muttli closed 7 years ago

muttli commented 9 years ago

I've written an authentication helper that authenticates using Ember-Simple-Auth.

import Ember from 'ember';
import Application from '../../app';
import Router from '../../router';
import config from '../../config/environment';

// Ember simple auth test helpers
import initializeTestHelpers from 'simple-auth-testing/test-helpers';
initializeTestHelpers();

var authenticator = {
  authenticate: function(application) {
    return new Ember.RSVP.Promise(function(resolve) {
      var data = {identification: 'email', password: 'password'};
      var OAuth2 = application.__container__.lookup('simple-auth-authenticator:oauth2-password-grant');

      OAuth2.reopen({
        makeRequest: function(url, data) {
          data.client_id = 'copeit-infoscreen';
          return this._super(url, data);
        }
      });

      expect(currentSession().isAuthenticated).to.be.equal(false, 'expected currentSession() to be false');
      currentSession().authenticate('simple-auth-authenticator:oauth2-password-grant', data).then(function() {
        expect(currentSession().isAuthenticated).to.be.equal(true, 'expected currentSession() to be true');
        resolve();
      });
    });
  }
}

export default authenticator;

The authentication is working, but when using it in a test, It skips to afterEach before the test is completed.

import { describe, it, beforeEach, afterEach } from 'mocha';
import { expect } from 'chai';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import authenticator from '../helpers/authenticator';

// Ember simple auth test helpers
import initializeTestHelpers from 'simple-auth-testing/test-helpers';
initializeTestHelpers();

describe.only('Acceptance: Routing', function() {

  var application;

  beforeEach(function() {
    application = startApp();
  });

  afterEach(function() {
    if (application && !application.isDestroyed && !application.isDestroying) {
      Ember.run(application, 'destroy');
    }
  });

  it('User gets redirected to attempted transitioned page after signing in', function(done) {
    visit('/controlpanel');
    andThen(function() {
      expect(currentPath()).to.equal('signin');
      authenticator.authenticate(application);
    });
    andThen(function() {
      expect(currentPath()).to.equal('controlpanel');
      done();
    })
  })
});

afterEach is called, and thus the application is destroyed by the time it checks if currentPath() is controlpanel

Turbo87 commented 7 years ago

@taheilo there are a few issues with your code examples above:

As this doesn't seem to be an issue with ember-cli-mocha itself I'm closing this issue for now.