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

Fix order dependence in tests. #252

Merged
merged 6 commits into from
Dec 5, 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
2 changes: 2 additions & 0 deletions addon-test-support/@ember/test-helpers/teardown-context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { run, next } from '@ember/runloop';
import { _teardownPromiseListeners } from './ext/rsvp';
import { _teardownAJAXHooks } from './settled';
import { unsetContext } from './setup-context';
import { Promise } from 'rsvp';
import Ember from 'ember';

Expand All @@ -16,6 +17,7 @@ export default function(context) {
run(owner, 'destroy');
Ember.testing = false;

unsetContext();
resolve(context);
});
});
Expand Down
35 changes: 25 additions & 10 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ import { registerDeprecationHandler } from '@ember/debug';
import AbstractTestLoader from 'ember-cli-test-loader/test-support/index';
import Ember from 'ember';

if (QUnit.config.seed) {
QUnit.config.reorder = false;
}

let moduleLoadFailures = [];
let cleanupFailures = [];

QUnit.done(function() {
if (moduleLoadFailures.length) {
throw new Error('\n' + moduleLoadFailures.join('\n'));
}

if (cleanupFailures.length) {
throw new Error('\n' + cleanupFailures.join('\n'));
}
});

class TestLoader extends AbstractTestLoader {
Expand Down Expand Up @@ -42,23 +51,29 @@ QUnit.testStart(function() {
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`
);
let message = `Ember.testing should be reset after test has completed. ${module}: ${
name
} did not reset Ember.testing`;
cleanupFailures.push(message);

// eslint-disable-next-line
console.error(message);
Ember.testing = false;
}

// this is used to ensure that the testing container is always reset properly
let testElementContainer = document.getElementById('ember-testing-container');
let actual = testElementContainer.innerHTML;
let expected = `<div id="ember-testing"></div>`;
if (actual !== expected) {
throw new Error(
`Expected #ember-testing-container to be reset after ${module}: ${name}, but was \`${
actual
}\``
);
let message = `Expected #ember-testing-container to be reset after ${module}: ${
name
}, but was \`${actual}\``;
cleanupFailures.push(message);

// eslint-disable-next-line
console.error(message);
testElementContainer.innerHTML = expected;
}
});

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/legacy-0-6-x/test-module-for-acceptance-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ moduleForAcceptance('TestModuleForAcceptance | Lifecycle', {

beforeSetup(assert) {
assert.expect(6);
assert.strictEqual(getContext(), undefined);
assert.ok(getContext() === undefined, 'precond - getContext() was reset');

setResolverRegistry({
'router:main': EmberRouter.extend({ location: 'none' }),
Expand Down
1 change: 1 addition & 0 deletions tests/unit/legacy-0-6-x/test-module-for-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ test('deprecation is not raised', function(assert) {
module('moduleForComponent: can be invoked with the component name and description', {
beforeEach(assert) {
var done = assert.async();
setupRegistry();
testModule = new TestModuleForComponent('pretty-color', 'PrettyColor', {
unit: true,
});
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/legacy-0-6-x/test-module-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ test("subject's created in a test are destroyed", function(assert) {
});

moduleFor('component:x-foo', 'component:x-foo -- without needs or `integration: true`', {
beforeSetup: setupRegistry(),
beforeSetup: () => setupRegistry(),
});

test('knows nothing about our non-subject component', function(assert) {
Expand All @@ -170,7 +170,7 @@ test('knows nothing about our non-subject component', function(assert) {
});

moduleFor('component:x-foo', 'component:x-foo -- when needing another component', {
beforeSetup: setupRegistry(),
beforeSetup: () => setupRegistry(),
needs: ['component:not-the-subject'],
});

Expand Down Expand Up @@ -337,6 +337,7 @@ var contexts, testModule;
QUnit.module('context can be provided to TestModule', {
beforeEach() {
contexts = [this];
setupRegistry();
testModule = new TestModule('component:x-foo', 'Foo', {
setup() {
contexts.push(this);
Expand Down Expand Up @@ -383,7 +384,9 @@ test('`toString` returns the test subject', function(assert) {
);
});

moduleFor('component:x-foo', 'ember-testing resets to empty value');
moduleFor('component:x-foo', 'ember-testing resets to empty value', {
beforeSetup: setupRegistry,
});

test('sets ember-testing content to "foobar"', function(assert) {
assert.expect(0);
Expand All @@ -401,7 +404,7 @@ test('sets ember-testing content to "<div>foobar</div>"', function(assert) {
assert.expect(1);
document.getElementById('ember-testing').innerHTML = '<div>foobar</div>';

testModule = new TestModule('component:x-foo', 'Foo');
testModule = new TestModule('component:x-foo', 'Foo', { beforeSetup: setupRegistry });
testModule.setContext(this);
return testModule
.setup(...arguments)
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/setup-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module('setupContext', function(hooks) {
return;
}

hooks.before(function() {
hooks.beforeEach(function() {
setResolverRegistry({
'service:foo': Service.extend({ isFoo: true }),
});
Expand All @@ -35,9 +35,7 @@ module('setupContext', function(hooks) {
await teardownContext(context);
context = undefined;
}
});

hooks.after(function() {
setApplication(application);
setResolver(resolver);
});
Expand Down
17 changes: 11 additions & 6 deletions tests/unit/teardown-context-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import Service from '@ember/service';
import { setupContext, teardownContext } from 'ember-test-helpers';
import { getContext, setupContext, teardownContext } from 'ember-test-helpers';
import { setResolverRegistry } from '../helpers/resolver';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import Ember from 'ember';
Expand All @@ -10,14 +10,11 @@ module('teardownContext', function(hooks) {
return;
}

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

let context;
hooks.beforeEach(function() {
context = {};
return setupContext(context);
});
Expand All @@ -40,4 +37,12 @@ module('teardownContext', function(hooks) {

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

test('it unsets the context', async function(assert) {
assert.strictEqual(getContext(), context, 'precond');

await teardownContext(context);

assert.strictEqual(getContext(), undefined, 'context is unset');
});
});