/
setup-no-deprecations.js
66 lines (58 loc) · 2.02 KB
/
setup-no-deprecations.js
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
import QUnit from 'qunit';
import { registerDeprecationHandler } from '@ember/debug';
let isRegistered = false;
let deprecations = new Set();
let expectedDeprecations = new Set();
// Ignore deprecations that are not caused by our own code, and which we cannot fix easily.
const ignoredDeprecations = [
/Versions of modifier manager capabilities prior to 3\.22 have been deprecated/,
/Usage of the Ember Global is deprecated./,
/import .* directly from/,
];
export default function setupNoDeprecations({ beforeEach, afterEach }) {
beforeEach(function () {
deprecations.clear();
expectedDeprecations.clear();
if (!isRegistered) {
registerDeprecationHandler((message, options, next) => {
if (!ignoredDeprecations.some((regex) => message.match(regex))) {
deprecations.add(message);
}
next(message, options);
});
isRegistered = true;
}
});
afterEach(function (assert) {
// guard in if instead of using assert.equal(), to not make assert.expect() fail
if (deprecations.size > expectedDeprecations.size) {
assert.ok(
false,
`Expected ${expectedDeprecations.size} deprecations, found: ${[...deprecations]
.map((msg) => `"${msg}"`)
.join(', ')}`
);
}
});
QUnit.assert.deprecations = function (count) {
if (count === undefined) {
this.ok(deprecations.size, 'Expected deprecations during test.');
} else {
this.equal(deprecations.size, count, `Expected ${count} deprecation(s) during test.`);
}
deprecations.forEach((d) => expectedDeprecations.add(d));
};
QUnit.assert.deprecationsInclude = function (expected) {
let found = [...deprecations].find((deprecation) => deprecation.includes(expected));
this.pushResult({
result: !!found,
actual: deprecations,
message: `expected to find \`${expected}\` deprecation. Found ${[...deprecations]
.map((d) => `"${d}"`)
.join(', ')}`,
});
if (found) {
expectedDeprecations.add(found);
}
};
}