-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
validateErrorHandler
utility.
Ensures that `Ember.onerror` is setup that properly re-throws when testing. This is intended to be used by test framework hosts (or other libraries) to ensure that `Ember.onerror` is properly configured. Without a check like this, `Ember.onerror` could _easily_ swallow all errors and make it _seem_ like everything is just fine (and have green tests) when in reality everything is on fire... Example implementation for `ember-qunit`: ```js import { validateErrorHandler } from '@ember/test-helpers'; test('Ember.onerror is functioning properly', function(assert) { let result = validateErrorHandler(); assert.ok(result.isValid, result.message); }); ```
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
addon-test-support/@ember/test-helpers/validate-error-handler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Ember from 'ember'; | ||
const VALID = Object.freeze({ isValid: true, message: null }); | ||
const INVALID = Object.freeze({ | ||
isValid: false, | ||
message: 'error handler should have re-thrown the provided error', | ||
}); | ||
|
||
export default function(callback = Ember.onerror) { | ||
if (callback === undefined) { | ||
return VALID; | ||
} | ||
|
||
let error = new Error('Error handler validation error!'); | ||
|
||
let originalEmberTesting = Ember.testing; | ||
Ember.testing = true; | ||
try { | ||
callback(error); | ||
} catch (e) { | ||
if (e === error) { | ||
return VALID; | ||
} | ||
} finally { | ||
Ember.testing = originalEmberTesting; | ||
} | ||
|
||
return INVALID; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { module, test } from 'qunit'; | ||
import Ember from 'ember'; | ||
|
||
import { validateErrorHandler } from '@ember/test-helpers'; | ||
|
||
module('validateErrorHandler', function(hooks) { | ||
hooks.beforeEach(function(assert) { | ||
assert.valid = result => { | ||
assert.deepEqual(result, { | ||
isValid: true, | ||
message: null, | ||
}); | ||
}; | ||
|
||
assert.invalid = result => { | ||
assert.deepEqual(result, { | ||
isValid: false, | ||
message: 'error handler should have re-thrown the provided error', | ||
}); | ||
}; | ||
}); | ||
|
||
module('with a passed in callback', function() { | ||
test('invokes the provided callback', function(assert) { | ||
assert.expect(1); | ||
|
||
validateErrorHandler(function() { | ||
assert.ok(true, 'error handler was invoked'); | ||
}); | ||
}); | ||
|
||
test('considers handler missing to be a valid handler', function(assert) { | ||
let result = validateErrorHandler(); | ||
|
||
assert.valid(result); | ||
}); | ||
|
||
test('when the provided function does _not_ rethrow it is invalid', function(assert) { | ||
let result = validateErrorHandler(function() {}); | ||
|
||
assert.invalid(result); | ||
}); | ||
|
||
test('when the provided function does rethrow it is valid', function(assert) { | ||
let result = validateErrorHandler(function(error) { | ||
throw error; | ||
}); | ||
|
||
assert.valid(result); | ||
}); | ||
}); | ||
|
||
module('without a passed in callback', function(hooks) { | ||
hooks.beforeEach(function() { | ||
this.originalOnerror = Ember.onerror; | ||
}); | ||
|
||
hooks.afterEach(function() { | ||
Ember.onerror = this.originalOnerror; | ||
}); | ||
|
||
test('invokes the provided callback', function(assert) { | ||
assert.expect(1); | ||
|
||
Ember.onerror = function() { | ||
assert.ok(true, 'error handler was invoked'); | ||
}; | ||
|
||
validateErrorHandler(); | ||
}); | ||
|
||
test('considers handler missing to be a valid handler', function(assert) { | ||
Ember.onerror = undefined; | ||
let result = validateErrorHandler(); | ||
|
||
assert.valid(result); | ||
}); | ||
|
||
test('when the provided function does _not_ rethrow it is invalid', function(assert) { | ||
Ember.onerror = function() {}; | ||
let result = validateErrorHandler(); | ||
|
||
assert.invalid(result); | ||
}); | ||
|
||
test('when the provided function does rethrow it is valid', function(assert) { | ||
Ember.onerror = function(error) { | ||
throw error; | ||
}; | ||
|
||
let result = validateErrorHandler(); | ||
|
||
assert.valid(result); | ||
}); | ||
}); | ||
}); |