Skip to content

Commit cde065f

Browse files
committed
WIP
1 parent 9286e89 commit cde065f

10 files changed

+531
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { getDeprecationsForCallback, getDeprecations } from '@ember/test-helpers';
2+
import checkMatcher from './utils/check-matcher';
3+
4+
export default function expectDeprecation(cb, matcher) {
5+
const test = deprecations => {
6+
const matchedDeprecations = deprecations.filter(deprecation => {
7+
return checkMatcher(deprecation.message, matcher);
8+
});
9+
10+
this.pushResult({
11+
result: matchedDeprecations.length !== 0,
12+
actual: matchedDeprecations,
13+
expected: null,
14+
message: 'Expected deprecations during test, but no deprecations were found.'
15+
});
16+
}
17+
18+
if (typeof cb !== 'function') {
19+
test(getDeprecations());
20+
} else {
21+
const maybeThenable = getDeprecationsForCallback(cb);
22+
if (maybeThenable !== null && typeof maybeThenable === 'object' && typeof maybeThenable.then === 'function') {
23+
return maybeThenable.then(test);
24+
} else {
25+
test(maybeThenable);
26+
}
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { end, _currentRunloop, _hasScheduledTimers, _cancelTimers } from '@ember/runloop';
2+
3+
export default function expectNoRunloop() {
4+
if (_currentRunLoop) {
5+
this.pushResult({
6+
result: false,
7+
actual: run.currentRunLoop,
8+
expected: null,
9+
message: 'Should not be in a run loop at end of test'
10+
});
11+
12+
while (_currentRunLoop) {
13+
end();
14+
}
15+
}
16+
17+
if (_hasScheduledTimers()) {
18+
this.pushResult({
19+
result: false,
20+
actual: true,
21+
expected: false,
22+
message: 'Ember run should not have scheduled timers at end of test'
23+
});
24+
25+
_cancelTimers();
26+
}
27+
}

addon-test-support/asserts/no-depreactions.js renamed to addon-test-support/asserts/no-deprecations.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { getDeprecations } from '@ember/test-helpers';
2-
import toAssertionMessage from './utils/to-assertion-message';
32

43
export default function noDeprecations() {
54
this.deepEqual(
6-
getDeprecations().map(toAssertionMessage),
5+
getDeprecations(),
76
[],
87
'Expected no deprecations during test.'
98
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function checkMatcher(message, matcher) {
2+
if (typeof matcher === 'string') {
3+
return includes(message, matcher);
4+
} else if (matcher instanceof RegExp) {
5+
return !!message.match(matcher);
6+
} else if (matcher) {
7+
throw new Error(`[ember-qunit] can only match Strings and RegExps. "${typeof matcher}" was provided.`);
8+
}
9+
10+
// No matcher always returns true. Makes the code easier elsewhere.
11+
return true;
12+
}

addon-test-support/asserts/warning.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { checkMatcher } from './utils';
2+
3+
4+
export default function() {
5+
let warnings;
6+
7+
QUnit.testStart(function() {
8+
warnings = [];
9+
});
10+
11+
Ember.Debug.registerWarnHandler(function(message, options, next) {
12+
// It's possible for warnings to trigger before the test has started.
13+
if (warnings) {
14+
warnings.push({ message, options });
15+
}
16+
next(message, options);
17+
});
18+
19+
function assertWarnings(qunitAssert, matcher) {
20+
let matchedWarnings = warnings.filter(warning => {
21+
return checkMatcher(warning.message, matcher);
22+
});
23+
qunitAssert.pushResult({
24+
result: matchedWarnings.length !== 0,
25+
actual: matchedWarnings,
26+
expected: null,
27+
message: 'Expected warnings during test, but no warnings were found.'
28+
});
29+
}
30+
31+
function assertNoWarnings(qunitAssert) {
32+
let warningStr = warnings.reduce((a, b) => {
33+
return `${b}${a.message}\n`;
34+
}, '');
35+
36+
qunitAssert.pushResult({
37+
result: warnings.length === 0,
38+
actual: warnings,
39+
expected: [],
40+
message: `Expected no warnings during test, but warnings were found.\n${warningStr}`
41+
});
42+
}
43+
44+
QUnit.assert.expectWarning = function(cb, matcher) {
45+
let originalWarnings = warnings;
46+
47+
if (typeof cb !== 'function') {
48+
matcher = cb;
49+
cb = null;
50+
}
51+
52+
if (cb) {
53+
warnings = [];
54+
cb();
55+
}
56+
57+
assertWarnings(this, matcher);
58+
warnings = originalWarnings;
59+
};
60+
61+
QUnit.assert.expectNoWarning = function(cb) {
62+
let originalWarnings = warnings;
63+
64+
if (cb) {
65+
warnings = [];
66+
cb();
67+
}
68+
69+
assertNoWarnings(this);
70+
warnings = originalWarnings;
71+
};
72+
}

addon-test-support/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@ let waitForSettled = true;
2929

3030
import deprecationsInclude from './asserts/deprecations-include';
3131
import deprecations from './asserts/deprecations';
32-
import noDeprecations from './asserts/no-depreactions';
32+
import noDeprecations from './asserts/no-deprecations';
33+
import expectDeprecation from './asserts/expect-deprecation';
34+
import expectNoRunloop from './asserts/expect-no-runloop';
35+
import expectWarning from './asserts/expect-warning';
3336

3437
export function setup(assert) {
38+
// TODO: decide which of these we should keep, which depreacte and which drop.
3539
assert.deprecationsInclude = deprecationsInclude;
3640
assert.deprecations = deprecations;
41+
assert.expectNoDeprecations = noDeprecations; // compat
3742
assert.noDeprecations = noDeprecations;
43+
assert.expectDeprecation = expectDeprecation; // compat
44+
assert.expectNoRunloop = expectNoRunloop; // compat but fixed name
45+
// around for compat
46+
assert.exepectNoRunLoop = expectNoRunloop; // compat but wrong camelization
3847
}
3948

4049
export function setupTest(hooks, _options) {
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { module, test } from 'qunit';
2+
import expectAssertion from 'ember-qunit/asserts/expect-assertion';
3+
import { assert as emberAssert } from '@ember/debug';
4+
5+
module('expectAssertion', function(hooks) {
6+
let mockAssert;
7+
8+
hooks.beforeEach(() => {
9+
mockAssert = {
10+
pushedResults: [],
11+
expectAssertion
12+
};
13+
});
14+
15+
test('called with assert', function(assert) {
16+
mockAssert.expectAssertion(() => {
17+
emberAssert('testing assert');
18+
});
19+
20+
assert.ok(mockAssert.pushedResults[0].result, '`expectAssertion` captured deprecation call');
21+
});
22+
23+
test('called without deprecation', function(assert) {
24+
mockAssert.expectAssertion(() => {
25+
emberAssert('testing assert', true);
26+
});
27+
28+
assert.notOk(mockAssert.pushedResults[0].result, '`expectAssertion` logged failed result');
29+
});
30+
31+
test('called with deprecation and matched assert', function(assert) {
32+
mockAssert.expectAssertion(() => {
33+
emberAssert('testing assert');
34+
}, /testing/);
35+
36+
assert.ok(mockAssert.pushedResults[0].result, '`expectAssertion` captured deprecation call');
37+
});
38+
39+
test('called with deprecation and unmatched assert', function(assert) {
40+
mockAssert.expectAssertion(() => {
41+
emberAssert('testing assert');
42+
}, /different/);
43+
44+
assert.notOk(mockAssert.pushedResults[0].result, '`expectAssertion` logged failed result');
45+
});
46+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { module, test } from 'qunit';
2+
import expectDeprecation from 'ember-qunit/asserts/expect-deprecation';
3+
import { deprecate } from '@ember/debug';
4+
5+
// ............................................................
6+
// Deprecation outside of a test. Should not cause test failures.
7+
deprecate('Deprecation outside of a test', false, { id: 'deprecation-test', until: '3.0.0' });
8+
// ............................................................
9+
10+
module('expectDeprecation', function(hooks) {
11+
let mockAssert;
12+
13+
hooks.beforeEach(() => {
14+
mockAssert = {
15+
pushedResults: [],
16+
expectDeprecation,
17+
};
18+
});
19+
20+
test('expectDeprecation called after test and with deprecation', function(assert) {
21+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
22+
23+
mockAssert.expectDeprecation();
24+
25+
assert.ok(mockAssert.pushedResults[0].result, '`expectDeprecation` captured deprecation call');
26+
});
27+
28+
test('expectDeprecation called after test and without deprecation', function(assert) {
29+
mockAssert.expectDeprecation();
30+
assert.notOk(mockAssert.pushedResults[0].result, '`expectDeprecation` logged failed result');
31+
});
32+
33+
test('expectDeprecation called with callback and with deprecation', function(assert) {
34+
mockAssert.expectDeprecation(() => {
35+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
36+
});
37+
38+
assert.ok(mockAssert.pushedResults[0].result, '`expectDeprecation` captured deprecation call');
39+
});
40+
41+
test('expectDeprecation called with callback and without deprecation', function(assert) {
42+
mockAssert.expectDeprecation(() => { });
43+
assert.notOk(mockAssert.pushedResults[0].result, '`expectDeprecation` logged failed result');
44+
});
45+
46+
test('expectDeprecation called with callback and after test', function(assert) {
47+
mockAssert.expectDeprecation(() => {
48+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
49+
});
50+
51+
mockAssert.expectDeprecation();
52+
assert.ok(mockAssert.pushedResults[0].result, 'first `expectDeprecation` captured deprecation call');
53+
assert.notOk(mockAssert.pushedResults[1].result, 'second `expectDeprecation` logged failed result');
54+
});
55+
56+
test('expectDeprecation called after test, with matcher and matched deprecation', function(assert) {
57+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
58+
59+
mockAssert.expectDeprecation(/Something deprecated/);
60+
assert.ok(mockAssert.pushedResults[0].result, '`expectDeprecation` captured deprecation call');
61+
});
62+
63+
test('expectDeprecation called after test, with matcher and unmatched deprecation', function(assert) {
64+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
65+
66+
mockAssert.expectDeprecation(/different deprecation/);
67+
assert.notOk(mockAssert.pushedResults[0].result, '`expectDeprecation` logged failed result');
68+
});
69+
70+
test('expectDeprecation called with callback, matcher and matched deprecation', function(assert) {
71+
mockAssert.expectDeprecation(() => {
72+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
73+
}, /Something deprecated/);
74+
75+
assert.ok(mockAssert.pushedResults[0].result, '`expectDeprecation` captured deprecation call');
76+
});
77+
78+
test('expectDeprecation called with callback, matcher and unmatched deprecation', function(assert) {
79+
mockAssert.expectDeprecation(() => {
80+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
81+
}, /different deprecation/);
82+
83+
assert.notOk(mockAssert.pushedResults[0].result, '`expectDeprecation` logged failed result');
84+
});
85+
86+
test('expectNoDeprecation called after test and without deprecation', function(assert) {
87+
assert.expectNoDeprecation();
88+
assert.ok(mockAssert.pushedResults[0].result, '`expectNoDeprecation` caught no deprecation');
89+
});
90+
91+
test('expectNoDeprecation called after test and with deprecation', function(assert) {
92+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
93+
94+
assert.expectNoDeprecation();
95+
assert.notOk(mockAssert.pushedResults[0].result, '`expectNoDeprecation` caught logged failed result');
96+
});
97+
98+
test('expectNoDeprecation called with callback and with deprecation', function(assert) {
99+
assert.expectNoDeprecation(() => {
100+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
101+
});
102+
103+
assert.notOk(mockAssert.pushedResults[0].result, '`expectNoDeprecation` caught logged failed result');
104+
});
105+
106+
test('expectNoDeprecation called with callback and without deprecation', function(assert) {
107+
assert.expectNoDeprecation(() => { });
108+
assert.ok(mockAssert.pushedResults[0].result, '`expectNoDeprecation` caught no deprecation');
109+
});
110+
111+
test('expectNoDeprecation called with callback and after test', function(assert) {
112+
assert.expectNoDeprecation(() => {
113+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
114+
});
115+
116+
assert.expectNoDeprecation();
117+
assert.notOk(mockAssert.pushedResults[0].result, 'first `expectNoDeprecation` caught logged failed result');
118+
assert.ok(mockAssert.pushedResults[1].result, 'second `expectNoDeprecation` caught no deprecation');
119+
});
120+
121+
test('expectDeprecation with regex matcher', function(assert) {
122+
mockAssert.expectDeprecation(() => {
123+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
124+
}, /Somethi[a-z ]*ecated/);
125+
mockAssert.expectDeprecation(() => {
126+
deprecate('/Something* deprecated/', false, { id: 'deprecation-test', until: '3.0.0' });
127+
}, /Something* deprecated/);
128+
129+
assert.ok(mockAssert.pushedResults[0].result, '`expectDeprecation` matched RegExp');
130+
assert.notOk(mockAssert.pushedResults[1].result, '`expectDeprecation` didn\'t RegExp as String match');
131+
});
132+
133+
test('expectDeprecation with string matcher', function(assert) {
134+
mockAssert.expectDeprecation(() => {
135+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
136+
}, 'Something');
137+
138+
mockAssert.expectDeprecation(() => {
139+
deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' });
140+
}, 'Something.*');
141+
142+
assert.ok(mockAssert.pushedResults[0].result, '`expectDeprecation` captured deprecation for partial String match');
143+
assert.notOk(mockAssert.pushedResults[1].result, '`expectDeprecation` didn\'t test a String match as RegExp');
144+
});
145+
});

0 commit comments

Comments
 (0)