-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test helpers wait
and andThen
not waiting after jQuery 3 upgrade
#14566
Comments
One solution: the app developer can wrap any jQuery promise interactions with This puts a heavy burden on the app developer, and doesn't help at all if jQuery promises are used internally (and not exposed) by third-party code. Another solution: make Ember aware of all jQuery promises. This can be done using some hacks. Add to
|
@dwickern can you create a working code example to demonstrate the issue? Perhaps an ember-twiddle ? |
@jordpo posted this repro in ember slack: https://github.com/jordpo/ember-test-issue With jQuery 3:
With jQuery 2:
|
I'm actually also having this issue when using regular
The weird this is that I'm wrapping the ajax request in an RSVP promise, so there shouldn't be any incompatibilities that i know of. Below is my code: let self = this;
return new Ember.RSVP.Promise((resolve, reject) => {
Ember.$.ajax({
method: "POST",
url: "/api/v1/sessions",
data: JSON.stringify(options),
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false
}).then((res) => {
let { user } = res;
Ember.$.ajaxSetup({
headers: {
"Authorization": `Bearer ${res.jwt}`
}
});
if (user) {
Ember.run(self, function() {
self.get('segment').identifyUser(user.id, { name: `${user.firstName} ${user.lastName}` });
self.get('store').pushPayload('user', { user });
});
}
resolve(res);
}, (xhr/*, status, err*/) => {
console.log("authenticate error", arguments);
return reject(xhr.responseJSON);
}); |
@validkeys I was confused about this as well. I thought wrapping the jQuery promises with RSVP promises would solve the problem, but it doesn't help.
If you changed your code like this, it would work:
|
@dwickern the current release of Ember (2.9.1) seems to indicate jQuery v3 should be supported, see https://github.com/emberjs/ember.js/blob/v2.9.1/config/package_manager_files/package.json#L11-L13 However, it appears that jQuery.ajax in v3 may not be compatible with the wait test helpers. |
@dwickern I tried to make an ember-twiddle example see: https://ember-twiddle.com/67dc4c9ffcc743610b9cff5bea7b80e0?openFiles=tests.acceptance.index.js%2C but can't get the test feature to just work for an acceptance test. |
@pixelhandler looks like it might be related to ember-cli/ember-cli#6247 Twiddle hasn't updated to [email protected] |
@dwickern I have a repo that I'm working on for ember-fetchjax which uses fetch or ajax. The repo is using jQuery 3.1.1. The ajax use does wrap jQuery.ajax with a Promise and the acceptance tests seem to work fine with
I think there are various options you can use with jQuery 3.1.x
|
I'm running into this same problem on an acceptance test against the login page using ember-simple-auth-token addon. The addon is actually wrapping the |
@pfmmfp You could try the workaround in #14566 (comment) |
Look at bug reported on emberjs emberjs/ember.js#14566 and also the migration guide http://jquery.com/upgrade-guide/3.0/#callback-invocation
Look at bug reported on emberjs emberjs/ember.js#14566 and also the migration guide http://jquery.com/upgrade-guide/3.0/#callback-invocation
As my PR shows (inspired on the jQuery migration guide), can't we migrate the In other words, the The ember-simple-auth library is also calling p.s.: I'm working on this issue with @pfmmfp if it helps |
More info explained [here](http://jquery.com/upgrade-guide/3.0/\#callback-invocation), which has been found based on [this ember issue](emberjs/ember.js#14566)
@morhook Apparently jQuery still calls
|
I'm facing this issue in acceptance test too. I use ESA 1.4.0 and leveraging ember-data 2.13.1 to fetch the user and load it into a current-user service. Attempting to work around it with import Ember from 'ember';
import DS from 'ember-data';
const { set, get } = Ember;
export default Ember.Service.extend({
init() {
this._super(...arguments);
if (Ember.testing) {
set(this, '_loading', false);
Ember.Test.registerWaiter(() => get(this, '_loading') === false);
}
},
session: Ember.inject.service('session'),
store: Ember.inject.service('store'),
userId: Ember.computed.oneWay('session.data.authenticated.user_id'),
permissions: Ember.computed('user.securityGroups.[]', {
get() {
let promise = new Ember.RSVP.Promise((resolve) => {
const permissions = get(this, 'user.securityGroups')
.mapBy('permissions')
.reduce((a, b) => a.concat(b))
.toArray();
Ember.run(() => resolve(permissions));
}, 'Current User Service: Permissions Promise');
if (Ember.testing) {
set(this, '_loading', true);
return DS.PromiseArray.create({
promise: promise.finally(() => set(this, '_loading', false))
});
}
return DS.PromiseArray.create({
promise: promise
});
}
}),
load() {
let promise = new Ember.RSVP.Promise((resolve, reject) => {
let store = get(this, 'store');
if (get(this, 'session.isAuthenticated')) {
let userId = get(this, 'userId');
const user = store.findRecord('user', userId);
Ember.run(() => resolve(user));
} else {
resolve();
}
}, 'Current User Service: loading user');
if (Ember.testing) {
set(this, '_loading', true);
let userPromiseObject = DS.PromiseObject.create({
promise: promise.finally(() => set(this, '_loading', false))
});
set(this, 'user', userPromiseObject);
}
let userPromiseObject = DS.PromiseObject.create({
promise: promise
});
set(this, 'user', userPromiseObject);
}
}); |
downgrading to jquery 2.2.4 doesn't seem to resolve the test failure. I'm using ember/ember-cli 2.14.1, ember-data 2.13.1. |
I've upgraded to Ember 3.1.0-beta.1. I've renamed my I'm seeing various tests finishing prematurely. Lots of One of the tests is particularly interesting in regard of this issue. It does this: Previously this was working fine: the test waited for logging in to complete and would resume after transitioning to another route. Now the test resumes while the app is still in the loading route and the login form says The interesting part is that I've edited that waiter to console.log('starting')
return this
.get('ajax')
.login(password)
.then(result => {
console.log('finishing')
return result
}) The resulting output is: Note how the number resets back to zero, and the waiter triggers several times before PS |
I've tried editing So maybe this issue isn't related to jQuery at all. My current impression is that we now have to apply a waiter to every promise manually, which is a heavy burden for developers, like @dwickern pointed out. |
I've tried using |
I've published a reproduction. Project: https://github.com/bread-maker/bread-maker-ember-frontend
Things to try:
Here's the same test before the Ember 3 upgrade. It does work successfully. |
@dwickern what do you think about closing out this issue? Now that we have the new ember test helpers we have an alternative to using |
I don't have this issue any more after switching from |
Upgrading to jQuery 3 can cause mysterious, intermittent test failures.
andThen
blocks will sometimes fire before the logic is done executing. This happens when using jQuery promises (either directly or through third-party jQuery UI components).Specifically, this change in jQuery 3:
http://jquery.com/upgrade-guide/3.0/#callback-invocation
This means that, even if an app uses jQuery promises in a synchronous manner (ie.
$.Deferred.resolve(value)
), the code runs asynchronously and must be tested asynchronously.Testing them asynchronously is not easy because Ember's wait helpers do not wait for jQuery promises (only route transitions and Ajax requests).
The text was updated successfully, but these errors were encountered: