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
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions addon-test-support/ember-qunit/async-timer-leak-detection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function detectPendingTimers(
hasPendingTimers,
pendingTimers,
testModule,
testName,
cancelTimers
) {
if (hasPendingTimers) {
pendingTimers.push(`${testModule}: ${testName}`);
cancelTimers();
}
}

export function reportPendingTimers(pendingTimers) {
if (pendingTimers.length > 0) {
throw new Error(
`ASYNC LEAKAGE DETECTED IN TESTS
The following (${pendingTimers.length}) tests setup a timer that was never torn down before the test completed: \n
${pendingTimers.join('\n')}
`
);
}
}
21 changes: 21 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,7 +25,11 @@ import {
setupApplicationContext,
teardownApplicationContext,
validateErrorHandler,
getSettledState,
} from '@ember/test-helpers';
import { detectPendingTimers, reportPendingTimers } from './async-timer-leak-detection';

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(
Expand Down Expand Up @@ -228,6 +233,18 @@ 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();

detectPendingTimers(hasPendingTimers, TESTS_WITH_LEAKY_ASYNC, module, name, run.cancelTimers);
});

QUnit.done(() => {
reportPendingTimers(TESTS_WITH_LEAKY_ASYNC);
});
}

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

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

if (options.startTests !== false) {
startTests();
}
Expand Down
17 changes: 10 additions & 7 deletions testem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ module.exports = {
'Chrome'
],
browser_args: {
Chrome: [
'--disable-gpu',
'--headless',
'--no-sandbox',
'--remote-debugging-port=9222',
'--window-size=1440,900'
]
Chrome: {
mode: 'ci',
args: [
'--disable-gpu',
'--headless',
'--no-sandbox',
'--remote-debugging-port=9222',
'--window-size=1440,900'
]
}
}
};
65 changes: 65 additions & 0 deletions tests/unit/setup-async-timer-leak-detection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { module, test } from 'qunit';
import { detectPendingTimers, reportPendingTimers } from 'ember-qunit/async-timer-leak-detection';

module('setupEmberOnerrorValidation', function() {
test('detectPendingTimers does not queue test info when leaky async timers not detected', function(assert) {
assert.expect(2);

let hasPendingTimers = false;
let pendingTimers = [];
let callCount = 0;
let cancelTimers = () => {
callCount++;
};

detectPendingTimers(hasPendingTimers, pendingTimers, '', '', cancelTimers);

assert.equal(callCount, 0, 'cancel was not called');
assert.equal(pendingTimers.length, 0, 'pending timers has no pending messages');
});

test('detectPendingTimers correctly queues test info when leaky async timers detected', function(assert) {
assert.expect(3);

let hasPendingTimers = true;
let pendingTimers = [];
let callCount = 0;
let cancelTimers = () => {
callCount++;
};

detectPendingTimers(hasPendingTimers, pendingTimers, 'module', 'test name', cancelTimers);

assert.equal(callCount, 1, 'cancel was called');
assert.equal(pendingTimers.length, 1, 'pending timers has a pending message');
assert.equal(
pendingTimers[0],
'module: test name',
'pending timers contains the correct message'
);
});

test('reportPendingTimers does not throw when no pending timers exist', function(assert) {
assert.expect(1);

reportPendingTimers([]);

assert.ok(true);
});

test('reportPendingTimers throws when pending timers exist', function(assert) {
assert.expect(1);

let pendingTimers = [];
pendingTimers.push('module: test name');

assert.throws(
function() {
reportPendingTimers(pendingTimers);
},
`ASYNC LEAKAGE DETECTED IN TESTS
The following (1) tests setup a timer that was never torn down before the test completed: \n
module: test name`
);
});
});