Skip to content

Commit 32cbcee

Browse files
committed
Make test context reusable during a test
In v2.0.0 @ember/test-helpers refactored to use @ember/destroyable for teardown which also meant that the application context was destroyed during teardown, where previously it was only cleaned up and the application was destroyed. In our application tests we simulate a page refresh by calling `teardownContext`` and `setupContext`. The destroyable change broke this functionality because the context is now marked as destroyed. This commit effectively reverts the change to use @ember/destroyable and adds tests to ensure that the context is not destroyed so that it can be reset during a test.
1 parent b7804e4 commit 32cbcee

5 files changed

Lines changed: 43 additions & 40 deletions

File tree

addon/src/setup-context.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Resolver } from '@ember/owner';
77
import { setOwner } from '@ember/application';
88

99
import buildOwner, { type Owner } from './build-owner.ts';
10-
import { _setupAJAXHooks, _teardownAJAXHooks } from './settled.ts';
10+
import { _setupAJAXHooks } from './settled.ts';
1111
import { _prepareOnerror } from './setup-onerror.ts';
1212
import Ember from 'ember';
1313
import {
@@ -19,10 +19,6 @@ import global from './global.ts';
1919
import { getResolver } from './resolver.ts';
2020
import { getApplication } from './application.ts';
2121
import getTestMetadata from './test-metadata.ts';
22-
import {
23-
registerDestructor,
24-
associateDestroyableChild,
25-
} from '@ember/destroyable';
2622
import {
2723
getDeprecationsForContext,
2824
getDeprecationsDuringCallbackForContext,
@@ -211,20 +207,6 @@ export function resumeTest(): void {
211207
context.resumeTest();
212208
}
213209

214-
/**
215-
@private
216-
@param {Object} context the test context being cleaned up
217-
*/
218-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
219-
function cleanup(context: BaseContext) {
220-
_teardownAJAXHooks();
221-
222-
// SAFETY: this is intimate API *designed* for us to override.
223-
(Ember as any).testing = false;
224-
225-
unsetContext();
226-
}
227-
228210
/**
229211
* Returns deprecations which have occurred so far for a the current test context
230212
*
@@ -410,8 +392,6 @@ export default function setupContext<T extends object>(
410392

411393
_backburner.DEBUG = true;
412394

413-
registerDestructor(context, cleanup);
414-
415395
_prepareOnerror(context);
416396

417397
return Promise.resolve()
@@ -438,8 +418,6 @@ export default function setupContext<T extends object>(
438418
return buildOwner(getApplication(), getResolver());
439419
})
440420
.then((owner) => {
441-
associateDestroyableChild(context, owner);
442-
443421
Object.defineProperty(context, 'owner', {
444422
configurable: true,
445423
enumerable: true,

addon/src/teardown-context.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { TestContext } from './setup-context';
2-
import settled from './settled.ts';
2+
import Ember from 'ember';
3+
import { unsetContext } from './setup-context.ts';
4+
import settled, { _teardownAJAXHooks } from './settled.ts';
35
import { _cleanupOnerror } from './setup-onerror.ts';
46
import { destroy } from '@ember/destroyable';
57

@@ -30,7 +32,14 @@ export default function teardownContext(
3032
.then(() => {
3133
_cleanupOnerror(context);
3234

33-
destroy(context);
35+
_teardownAJAXHooks();
36+
37+
// SAFETY: this is intimate API *designed* for us to override.
38+
(Ember as any).testing = false;
39+
40+
unsetContext();
41+
42+
destroy(context.owner);
3443
})
3544
.finally(() => {
3645
if (waitForSettled) {

test-app/tests/unit/setup-application-context-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,17 @@ module('setupApplicationContext', function (hooks) {
257257
'after click resolved',
258258
]);
259259
});
260+
261+
test('can reset context during a test', async function (assert) {
262+
await visit('/');
263+
assert.equal(currentURL(), '/');
264+
265+
await teardownContext(this);
266+
await setupContext(this);
267+
await setupApplicationContext(this);
268+
assert.equal(currentURL(), null);
269+
270+
await visit('/');
271+
assert.equal(currentURL(), '/');
272+
});
260273
});

test-app/tests/unit/setup-context-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,5 +943,19 @@ module('setupContext', function (hooks) {
943943
}
944944
}
945945
});
946+
947+
test('can reset context during a test', async function (assert) {
948+
assert.expect(1);
949+
950+
let context = await setupContext({});
951+
try {
952+
await teardownContext(context);
953+
context = await setupContext(context);
954+
} finally {
955+
await teardownContext(context);
956+
}
957+
958+
assert.ok(true, 'No error calling setupContext after teardownContext');
959+
});
946960
});
947961
});

test-app/tests/unit/teardown-context-test.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,17 @@ module('teardownContext', function (hooks) {
6565
assert.strictEqual(getContext(), undefined, 'context is unset');
6666
});
6767

68-
test('destroyables registered with the context are invoked', async function (assert) {
69-
registerDestructor(context, () => {
70-
assert.step('destructor was ran');
71-
});
72-
73-
assert.step('teardown started');
74-
68+
test('the owner is destroyed', async function (assert) {
7569
await teardownContext(context);
7670

77-
assert.step('teardown completed');
78-
79-
assert.verifySteps([
80-
'teardown started',
81-
'destructor was ran',
82-
'teardown completed',
83-
]);
71+
assert.ok(context.owner.isDestroyed);
8472
});
8573

86-
test('the owner is destroyed', async function (assert) {
74+
test('the context is not destroyed', async function (assert) {
8775
await teardownContext(context);
8876

8977
assert.ok(context.owner.isDestroyed);
78+
assert.notOk(context.isDestroyed);
9079
});
9180

9281
test('the application instance is destroyed and unwatched', async function (assert) {

0 commit comments

Comments
 (0)