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

Adding tests not settled detection support in tests #314

Merged
merged 8 commits into from
Feb 23, 2018
Merged
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions addon-test-support/ember-qunit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { module, test, skip, only, todo } from 'qunit';
export { loadTests } from './test-loader';

import { deprecate } from '@ember/debug';
import { run } from '@ember/runloop';
import { loadTests } from './test-loader';
import Ember from 'ember';
import QUnit from 'qunit';
Expand All @@ -24,8 +25,11 @@ import {
setupApplicationContext,
teardownApplicationContext,
validateErrorHandler,
getSettledState,
} from '@ember/test-helpers';

const TESTS_WITH_LEAKY_ASYNC = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should move this into the util file, no need to track it here and pass it around

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Not sure why I didn't see this....


export function setResolver() {
deprecate(
'`setResolver` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
Expand Down Expand Up @@ -228,6 +232,28 @@ export function setupEmberOnerrorValidation() {
});
}

export function setupAsyncTimerLeakDetection() {
QUnit.testDone(({ module, name }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making these callbacks separate (but private) exported functions will allow you to invoke them manually from within a single test and assert that the expected error is thrown....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. That will address my nagging guilt at the thought of pushing code with no tests!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, its the guilt that drives me...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split things up. Added tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this whole function the thing that is extracted? So that the resulting code would be:

QUnit.testDone(importedThingGoesHere);
QUnit.done(otherImportedThingGoesHere);

let { hasPendingTimers } = getSettledState();

if (hasPendingTimers) {
TESTS_WITH_LEAKY_ASYNC.push(`${module}: ${name}`);
run.cancelTimers();
}
});

QUnit.done(() => {
if (TESTS_WITH_LEAKY_ASYNC.length > 0) {
throw new Error(
`ASYNC LEAKAGE DETECTED IN TESTS
The following (${TESTS_WITH_LEAKY_ASYNC.length}) tests setup a timer that was never torn down before the test completed: \n
${TESTS_WITH_LEAKY_ASYNC.join('\n')}
`
);
}
});
}

/**
@method start
@param {Object} [options] Options to be used for enabling/disabling behaviors
Expand Down Expand Up @@ -265,6 +291,10 @@ export function start(options = {}) {
setupEmberOnerrorValidation();
}

if (options.setupAsyncTimerLeakDetection !== false) {
setupAsyncTimerLeakDetection();
}

if (options.startTests !== false) {
startTests();
}
Expand Down