Last active
August 15, 2024 15:14
-
-
Save pablobm/5ec0fee8a877badaca2c11bf4751e857 to your computer and use it in GitHub Desktop.
Ember.js: error state workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}, | |
}; |
Blog post detailing this workaround: https://www.runtastic.com/blog/en/tech/testing-error-states-ember/
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
Discussion around this issue: emberjs/ember.js#12791