Skip to content
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

Set Ember.testing only while actually running a test. #227

Merged
merged 1 commit into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions addon-test-support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ export { default as teardownContext } from './teardown-context';
export { default as setupRenderingContext, render, clearRender } from './setup-rendering-context';
export { default as teardownRenderingContext } from './teardown-rendering-context';
export { default as settled } from './settled';

import Ember from 'ember';
Ember.testing = true;
4 changes: 4 additions & 0 deletions addon-test-support/legacy-0-6-x/abstract-test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class {
}

setup(assert) {
Ember.testing = true;
return this.invokeSteps(this.setupSteps, this, assert).then(() => {
this.contextualizeCallbacks();
return this.invokeSteps(this.contextualizedSetupSteps, this.context, assert);
Expand All @@ -37,6 +38,9 @@ export default class {
.then(() => {
this.cache = null;
this.cachedCalls = null;
})
.finally(function() {
Ember.testing = false;
});
}

Expand Down
2 changes: 2 additions & 0 deletions addon-test-support/setup-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { set, setProperties, get, getProperties } from '@ember/object';
import buildOwner from './build-owner';
import { _setupPromiseListeners } from './ext/rsvp';
import { _setupAJAXHooks } from './settled';
import Ember from 'ember';

let __test_context__;

Expand All @@ -28,6 +29,7 @@ export function unsetContext() {
* - setting up RSVP promise integration
*/
export default function(context, options = {}) {
Ember.testing = true;
setContext(context);

let resolver = options.resolver;
Expand Down
2 changes: 2 additions & 0 deletions addon-test-support/teardown-context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { run } from '@ember/runloop';
import { _teardownPromiseListeners } from './ext/rsvp';
import { _teardownAJAXHooks } from './settled';
import Ember from 'ember';

export default function(context) {
let { owner } = context;
Expand All @@ -9,4 +10,5 @@ export default function(context) {
_teardownAJAXHooks();

run(owner, 'destroy');
Ember.testing = false;
}
6 changes: 5 additions & 1 deletion tests/helpers/module-for-acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { resolve } from 'rsvp';
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
import Ember from 'ember';

export default function(name, options = {}) {
module(name, {
beforeEach() {
Ember.testing = true;
this.application = startApp();

if (options.beforeEach) {
Expand All @@ -15,7 +17,9 @@ export default function(name, options = {}) {

afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return resolve(afterEach).then(() => destroyApp(this.application));
return resolve(afterEach)
.then(() => destroyApp(this.application))
.finally(() => (Ember.testing = false));
},
});
}
15 changes: 13 additions & 2 deletions tests/helpers/qunit-module-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ import QUnitTestAdapter from './qunit-test-adapter';
export default function qunitModuleFor(testModule) {
module(testModule.name, {
beforeEach(assert) {
if (Ember.testing) {
throw new Error('should not have Ember.testing === true in beforeEach');
}
Ember.Test.adapter = QUnitTestAdapter.create();
testModule.setContext(this);
return testModule.setup(assert);
return testModule.setup(assert).finally(() => {
if (!Ember.testing) {
throw new Error('should have Ember.testing === true after tests have started');
}
});
},
afterEach(assert) {
return testModule.teardown(assert);
return testModule.teardown(assert).finally(() => {
if (Ember.testing) {
throw new Error('should not have Ember.testing === true after tests have finished');
}
});
},
});
}
10 changes: 10 additions & 0 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import QUnit from 'qunit';
import { registerDeprecationHandler } from '@ember/debug';
import AbstractTestLoader from 'ember-cli-test-loader/test-support/index';
import Ember from 'ember';

let moduleLoadFailures = [];

Expand Down Expand Up @@ -38,6 +39,15 @@ QUnit.testStart(function() {
deprecations = [];
});

QUnit.testDone(function({ module, name }) {
// this is used to ensure that no tests accidentally leak `Ember.testing` state
if (Ember.testing) {
throw new Error(
`Ember.testing should be reset after test has completed. ${module}: ${name} did not reset Ember.testing`
);
}
});

QUnit.assert.noDeprecations = function(callback) {
let originalDeprecations = deprecations;
deprecations = [];
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/legacy-0-6-x/test-module-for-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ QUnit.module('moduleForComponent: handles errors thrown during setup', {
'correct error was thrown from module setup'
);
})
.finally(done);
.finally(() => {
done();
Ember.testing = false;
});
},
});

Expand Down
23 changes: 20 additions & 3 deletions tests/unit/setup-context-test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { module, test } from 'qunit';
import Service, { inject as injectService } from '@ember/service';
import { setupContext, getContext } from 'ember-test-helpers';
import { setupContext, teardownContext, getContext } from 'ember-test-helpers';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import { setResolverRegistry, createCustomResolver } from '../helpers/resolver';
import Ember from 'ember';

module('setupContext', function(hooks) {
if (!hasEmberVersion(2, 4)) {
return;
}

let context;
hooks.before(function() {
setResolverRegistry({
'service:foo': Service.extend({ isFoo: true }),
});
});

hooks.afterEach(function() {
if (context) {
teardownContext(context);
context = undefined;
}
});

module('without options', function(hooks) {
let context;
hooks.beforeEach(function() {
context = {};
setupContext(context);
Expand Down Expand Up @@ -122,7 +130,7 @@ module('setupContext', function(hooks) {

module('with custom options', function() {
test('it can specify a custom resolver', function(assert) {
let context = {};
context = {};
let resolver = createCustomResolver({
'service:foo': Service.extend({ isFoo: 'maybe?' }),
});
Expand All @@ -132,4 +140,13 @@ module('setupContext', function(hooks) {
assert.equal(instance.isFoo, 'maybe?', 'uses the custom resolver');
});
});

test('Ember.testing', function(assert) {
assert.notOk(Ember.testing, 'precond - Ember.testing is falsey before setup');

context = {};
setupContext(context);

assert.ok(Ember.testing, 'Ember.testing is truthy after setup');
});
});
9 changes: 9 additions & 0 deletions tests/unit/teardown-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Service from '@ember/service';
import { setupContext, teardownContext } from 'ember-test-helpers';
import { setResolverRegistry } from '../helpers/resolver';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import Ember from 'ember';

module('teardownContext', function(hooks) {
if (!hasEmberVersion(2, 4)) {
Expand Down Expand Up @@ -31,4 +32,12 @@ module('teardownContext', function(hooks) {
assert.ok(instance.isDestroyed, 'destroyed');
assert.ok(instance.isDestroying, 'destroying');
});

test('it sets Ember.testing to false', function(assert) {
assert.ok(Ember.testing, 'precond - Ember.testing is truthy');

teardownContext(context);

assert.notOk(Ember.testing, 'Ember.testing is falsey after teardown');
});
});