-
-
Notifications
You must be signed in to change notification settings - Fork 154
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
Changes from 3 commits
4924b49
a406a56
193bd8b
2643f84
f5eb5a6
3febb6b
0488804
a7e93c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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')} | ||
` | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 = []; | ||
|
||
export function setResolver() { | ||
deprecate( | ||
|
@@ -228,6 +233,18 @@ export function setupEmberOnerrorValidation() { | |
}); | ||
} | ||
|
||
export function setupAsyncTimerLeakDetection() { | ||
QUnit.testDone(({ module, name }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol, its the guilt that drives me... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split things up. Added tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -265,6 +282,10 @@ export function start(options = {}) { | |
setupEmberOnerrorValidation(); | ||
} | ||
|
||
if (options.setupAsyncTimerLeakDetection !== false) { | ||
setupAsyncTimerLeakDetection(); | ||
} | ||
|
||
if (options.startTests !== false) { | ||
startTests(); | ||
} | ||
|
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` | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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....