forked from emberjs/ember-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warnings.ts
109 lines (94 loc) · 3.57 KB
/
warnings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 {Function} [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: () => void
): 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 (!options || !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 (options && debugWarnings.includes(options.id)) {
debugger; // eslint-disable-line no-debugger
}
next.apply(null, [message, options]);
});
}
}