-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose deprecation primitives * `?disableWarnings` queryParam * `?debugWarnings` queryParam * `import { getWarnings, getWarningsDuringCallback } from ‘@ember/test-helpers’;` This is a second step in allowing us to deprecate https://github.com/workmanw/ember-qunit-assert-helpers in-favor of these helpers simply being provided by default. --- Context: This is motivated by 4.0 changes that are incompatible with the original add-on, and the time trade-off of resurrecting that add-on vs adopting its great ideas into mainline. Although users will need to upgrade their `@ember/test-helpers` + `ember-qunit` they will now get the original functionality, + async + bugfixes by default. I was lead down this path getting embroider working properly on `ember@canary` --- enables resolving emberjs/ember-string#259 amongst other related issues (basically anyone consuming ember-qunit-assert-helpers)
- Loading branch information
1 parent
ae7b1d4
commit 7312bfd
Showing
8 changed files
with
531 additions
and
113 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
14 changes: 14 additions & 0 deletions
14
addon-test-support/@ember/test-helpers/-internal/is-promise.ts
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,14 @@ | ||
/** | ||
* | ||
* detect if a value appears to be a promise | ||
* | ||
* @private | ||
* @param {any} [maybePromise] the value being considered to be a promise | ||
* @return {boolean} true if the value appears to be a promise, or false otherwise | ||
*/ | ||
export default function (maybePromise: any): boolean { | ||
return ( | ||
(maybePromise !== null && typeof maybePromise === 'object') || | ||
(typeof maybePromise === 'function' && typeof maybePromise.then === 'function') | ||
); | ||
} |
107 changes: 107 additions & 0 deletions
107
addon-test-support/@ember/test-helpers/-internal/warnings.ts
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,107 @@ | ||
import { BaseContext } from '../setup-context'; | ||
import { registerWarnHandler } from '@ember/debug'; | ||
import isPromise from './is-promise'; | ||
|
||
export interface WarningOptions { | ||
id?: string; | ||
} | ||
|
||
export interface Warning { | ||
message: string; | ||
options?: WarningOptions; | ||
} | ||
|
||
// the WARNINGS data structure which is used to weakly associated warnings with | ||
// the test context their occured within | ||
const WARNINGS = new WeakMap<BaseContext, Array<Warning>>(); | ||
|
||
/** | ||
* | ||
* Provides the list of warnings associated with a given base context; | ||
* | ||
* @private | ||
* @param {BaseContext} [context] the test context | ||
* @return {Array<Warning>} the warnings associated with the corresponding BaseContext; | ||
*/ | ||
export function getWarningsForContext(context: BaseContext): Array<Warning> { | ||
if (!context) { | ||
throw new TypeError( | ||
`[@ember/test-helpers] could not get warnings for an invalid test context: '${context}'` | ||
); | ||
} | ||
|
||
let warnings = WARNINGS.get(context); | ||
|
||
if (!Array.isArray(warnings)) { | ||
warnings = []; | ||
WARNINGS.set(context, warnings); | ||
} | ||
|
||
return warnings; | ||
} | ||
|
||
/** | ||
* | ||
* Provides the list of warnings associated with a given test context which | ||
* occured only while a the provided callback is executed. This callback can be | ||
* synchonous, or it can be an async function. | ||
* | ||
* @private | ||
* @param {BaseContext} [context] the test context | ||
* @param {CallableFunction} [callback] The callback that when executed will have its warnings recorded | ||
* @return {Array<Warning>} The warnings associated with the corresponding baseContext which occured while the CallbackFunction was executed | ||
*/ | ||
export function getWarningsDuringCallbackForContext( | ||
context: BaseContext, | ||
callback: CallableFunction | ||
): Array<Warning> | Promise<Array<Warning>> { | ||
if (!context) { | ||
throw new TypeError( | ||
`[@ember/test-helpers] could not get warnings for an invalid test context: '${context}'` | ||
); | ||
} | ||
|
||
const warnings = getWarningsForContext(context); | ||
const previousLength = warnings.length; | ||
|
||
const result = callback(); | ||
|
||
if (isPromise(result)) { | ||
return Promise.resolve(result).then(() => { | ||
return warnings.slice(previousLength); // only return warnings created as a result of the callback | ||
}); | ||
} else { | ||
return warnings.slice(previousLength); // only return warnings created as a result of the callback | ||
} | ||
} | ||
|
||
// This provides (when the environment supports) queryParam support for warnings: | ||
// * squelch warnings by name via: `/tests/index.html?disabledWarnings=this-property-fallback,some-other-thing` | ||
// * enable a debuggger when a warning by a specific name is encountered via: `/tests/index.html?debugWarnings=some-other-thing` when the | ||
if (typeof URLSearchParams !== 'undefined') { | ||
const queryParams = new URLSearchParams(document.location.search.substring(1)); | ||
const disabledWarnings = queryParams.get('disabledWarnings'); | ||
const debugWarnings = queryParams.get('debugWarnings'); | ||
|
||
// When using `/tests/index.html?disabledWarnings=this-property-fallback,some-other-thing` | ||
// those warnings will be squelched | ||
if (disabledWarnings) { | ||
registerWarnHandler((message, options, next) => { | ||
if (!disabledWarnings.includes(options.id)) { | ||
next.apply(null, [message, options]); | ||
} | ||
}); | ||
} | ||
|
||
// When using `/tests/index.html?debugWarnings=some-other-thing` when the | ||
// `some-other-thing` warning is triggered, this `debugger` will be hit` | ||
if (debugWarnings) { | ||
registerWarnHandler((message, options, next) => { | ||
if (debugWarnings.includes(options.id)) { | ||
debugger; // eslint-disable-line no-debugger | ||
} | ||
|
||
next.apply(null, [message, options]); | ||
}); | ||
} | ||
} |
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
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,23 @@ | ||
import { module, test } from 'qunit'; | ||
import isPromise from '@ember-test-helpers/-internal/is-promise'; | ||
|
||
module('isPromise', function () { | ||
test('detects promise-like like values', function (assert) { | ||
assert.ok(isPromise(new Promise(() => {}))); | ||
assert.ok(isPromise(Promise.resolve())); | ||
assert.ok(isPromise({ then() {} })); | ||
const functionObject = () => {}; | ||
functionObject.then = () => {}; | ||
assert.ok(isPromise(functionObject)); | ||
}); | ||
|
||
test('it if a value is not a promise-like value', function (assert) { | ||
assert.noOk(isPromise()); | ||
assert.noOk(isPromise(undefined)); | ||
assert.noOk(isPromise(null)); | ||
assert.noOk(isPromise(1)); | ||
assert.noOk(isPromise(NaN)); | ||
assert.noOk(isPromise({})); | ||
assert.noOk(isPromise(() => {})); | ||
}); | ||
}); |
Oops, something went wrong.