Handle expected failures in tests with Pretender


#1

Hi.

We’re using Pretender with QUnit tests with ember-cli.

Specifically, we want to check whether an action displays an error message in an error dialog on the page for some request.

We set Pretender to answer the request with status code 409 and that is working fine.

Alas, the test fails prematurely with two extra assertions having failed:

1. Error: Conflict@ 316 ms
Expected: 	true
Result: 	 false
Diff: 	 true false

2. TypeError: cyclic object value@ 319 ms
Expected: 	true
Result: 	        false
Diff: 	true false

The cause seems to be that Ember itself handles the failing AJAX promise spitting out these errors when in “testing” mode.

Using RSVP.off('error', Ember.RSVP.onerrorDefault); at the beginning of the test case disables this behaviour, but that function is not even documented and it feels a bit awkward.

Is there a better way to handle this scenario? How do you let the errors just pass through to the application?

Thanks!


#2

FYI, for anyone that finds this later. See https://github.com/simplabs/ember-simple-auth/issues/439

The problem is with LoginControllerMixin and AuthenticationControllerMixin. They have been deprecated, so stop using them and you don’t have to use this hack to “fix” the problem.


#3

I’m not entirely certain the core team would love this but here is how I’ve modified the setup/teardown for acceptance tests so I can do xhr failure scenarios

module('Acceptance | Some Test File', {
    beforeEach() {
        application = startApp();
        originalLoggerError = Ember.Logger.error;
        originalTestAdapterException = Ember.Test.adapter.exception;
        Ember.Logger.error = function() {};
        Ember.Test.adapter.exception = function() {};
    },
    afterEach() {
        Ember.Logger.error = originalLoggerError;
        Ember.Test.adapter.exception = originalTestAdapterException;
        Ember.run(application, 'destroy');
    }
});

Next I handle the error application side using a simple onError hook (w/ my ajax wrapper class)

Then in my ember-cli app I setup the following initializer to “set” the onError hook

import PromiseMixin from 'ember-promise/mixins/promise';
import errorFunction from 'myApp/utilities/error-function';

export function initialize() {
    PromiseMixin.onError = errorFunction;
} 

export default {
    name: 'global-error-handling',
    initialize: initialize
};

And finally what my error handler looks like in code

var errorFunction = (xhr, textStatus, errorThrown, message) => {
    if (xhr.status === 403) {
    }
};

export default errorFunction;