Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ember.js: error state workaround
// This is an example of use of `errorStateWorkaround`
import Ember from 'ember';
import { test } from 'qunit';
import moduleForAcceptance from 'my-app/tests/helpers/module-for-acceptance';
import errorStateWorkaround from 'my-app/tests/helpers/error-state-workaround';
moduleForAcceptance("Acceptance | errors", {
beforeEach: function() {
errorStateWorkaround.setup(err => {
// Return `true` if `err` is the error
// we expect, and `false` otherwise
});
},
afterEach: function() {
errorStateWorkaround.teardown();
},
});
test("Something that lands an error", function(assert) {
// Do something that would get the user to
// an error route or substate
andThen(function() {
// Assert that the error has ocurred as expected
});
});
// Ember annoyingly breaks acceptance tests that involve
// landing on an "error" substate. Until this is solved,
// this workaround helps.
//
// This involves temporariliy overriding Ember.Test.adapter.exception
// to make it not re-raise exceptions related to this
// error substate.
//
// Code is mostly pinched from https://github.com/emberjs/ember.js/issues/12791
// (see comment by @aquamme on May 11, 2016), adapted to
// be reusable.
//
// To use, import this module and call `setup` on the
// test's `beforeEach` hook. Then call `teardown` on
// `afterEach`:
//
// * `setup`: receives a callback that in turn
// will receive the raised error. The callback
// should return a truthy value if the passed error
// must be ignored, falsy otherwise. Ie: if the
// passed error is ok and simply part of the error
// chain expected to lead to the error substate,
// return true; otherwise return false.
//
// * `teardown`: doesn't take any arguments.
//
// Note that Ember.Test.adapter.exception will be overriden
// between the calls to `setup` and `teardown`. If you
// do any further override, this code will not work.
//
import Ember from 'ember';
export default {
oldAdapterException: null,
setup(isExpected) {
this.oldAdapterException = Ember.Test.adapter.exception;
Ember.Test.adapter.exception = err => {
if (!isExpected(err)) {
return this.oldAdapterException(err);
}
};
},
teardown() {
Ember.Test.adapter.exception = this.oldAdapterException;
},
};
@pablobm

This comment has been minimized.

Copy link
Owner Author

pablobm commented Sep 21, 2017

Discussion around this issue: emberjs/ember.js#12791

@pablobm

This comment has been minimized.

Copy link
Owner Author

pablobm commented Sep 21, 2017

@okket

This comment has been minimized.

Copy link

okket commented Sep 22, 2017

Is this workaround still needed with this fix/commit (see last comment in the discussion thread)? emberjs/ember.js#14898

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.