Skip to content

Commit

Permalink
Merge pull request #20210 from patricklx/patch-3
Browse files Browse the repository at this point in the history
fix hanging tests caused by legacy rsvp code
  • Loading branch information
wagenet authored Aug 18, 2023
2 parents 1cd116d + 5e0580d commit 5709138
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 100 deletions.
12 changes: 1 addition & 11 deletions packages/ember-testing/lib/ext/rsvp.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { RSVP } from '@ember/-internals/runtime';
import { _backburner } from '@ember/runloop';
import { isTesting } from '@ember/debug';
import { asyncStart, asyncEnd } from '../test/adapter';

RSVP.configure(
'async',
function (callback: (promise: Promise<unknown>) => void, promise: Promise<unknown>) {
// if schedule will cause autorun, we need to inform adapter
if (isTesting() && !_backburner.currentInstance) {
asyncStart();
_backburner.schedule('actions', () => {
asyncEnd();
callback(promise);
});
} else {
_backburner.schedule('actions', () => callback(promise));
}
_backburner.schedule('actions', () => callback(promise));
}
);

Expand Down
89 changes: 0 additions & 89 deletions packages/ember-testing/tests/ext/rsvp_test.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,6 @@
import RSVP from '../../lib/ext/rsvp';
import { getAdapter, setAdapter } from '../../lib/test/adapter';
import TestPromise, { getLastPromise } from '../../lib/test/promise';
import { _getCurrentRunLoop } from '@ember/runloop';
import { isTesting, setTesting } from '@ember/debug';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

const originalTestAdapter = getAdapter();
const originalTestingFlag = isTesting();

let asyncStarted = 0;
let asyncEnded = 0;

moduleFor(
'ember-testing RSVP',
class extends AbstractTestCase {
constructor() {
super();
setTesting(true);
setAdapter({
asyncStart() {
asyncStarted++;
},
asyncEnd() {
asyncEnded++;
},
});
}

teardown() {
asyncStarted = 0;
asyncEnded = 0;
setAdapter(originalTestAdapter);
setTesting(originalTestingFlag);
}

['@test given `Ember.testing = true`, correctly informs the test suite about async steps'](
assert
) {
let done = assert.async();
assert.expect(19);

assert.ok(!_getCurrentRunLoop(), 'expect no run-loop');

setTesting(true);

assert.equal(asyncStarted, 0);
assert.equal(asyncEnded, 0);

let user = RSVP.Promise.resolve({ name: 'tomster' });

assert.equal(asyncStarted, 0);
assert.equal(asyncEnded, 0);

user
.then(function (user) {
assert.equal(asyncStarted, 1);
assert.equal(asyncEnded, 1);

assert.equal(user.name, 'tomster');

return RSVP.Promise.resolve(1).then(function () {
assert.equal(asyncStarted, 1);
assert.equal(asyncEnded, 1);
});
})
.then(function () {
assert.equal(asyncStarted, 1);
assert.equal(asyncEnded, 1);

return new RSVP.Promise(function (resolve) {
setTimeout(function () {
assert.equal(asyncStarted, 1);
assert.equal(asyncEnded, 1);

resolve({ name: 'async tomster' });

assert.equal(asyncStarted, 2);
assert.equal(asyncEnded, 1);
}, 0);
});
})
.then(function (user) {
assert.equal(user.name, 'async tomster');
assert.equal(asyncStarted, 2);
assert.equal(asyncEnded, 2);
done();
});
}
}
);

moduleFor(
'TestPromise',
class extends AbstractTestCase {
Expand Down

0 comments on commit 5709138

Please sign in to comment.