Skip to content

Commit 53de824

Browse files
committed
WIP
1 parent 0bdf3c6 commit 53de824

17 files changed

+2030
-2127
lines changed

addon-test-support/assertions/deprecations-include.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { getDeprecations } from '@ember/test-helpers';
22

33
export default function deprecationsInclude(expected) {
4-
const deprecations = getDeprecations().map(deprecation => deprecation.message);
4+
const deprecations = getDeprecations().map(
5+
(deprecation) => deprecation.message
6+
);
57

68
this.pushResult({
79
result: deprecations.indexOf(expected) > -1,

addon-test-support/assertions/deprecations.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default async function deprecations(callback, expectedDeprecations) {
55

66
const operation = (deprecations) => {
77
this.deepEqual(
8-
deprecations.map(deprecation => deprecation.message),
8+
deprecations.map((deprecation) => deprecation.message),
99
expectedDeprecations,
1010
'Expected deprecations during test.'
1111
);
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1-
import { getDeprecationsForCallback, getDeprecations } from '@ember/test-helpers';
1+
import {
2+
getDeprecationsDuringCallback,
3+
getDeprecations,
4+
} from '@ember/test-helpers';
25
import checkMatcher from './utils/check-matcher';
36

47
export default function expectDeprecation(cb, matcher) {
5-
const test = deprecations => {
6-
const matchedDeprecations = deprecations.filter(deprecation => {
8+
const test = (deprecations, matcher) => {
9+
const matchedDeprecations = deprecations.filter((deprecation) => {
710
return checkMatcher(deprecation.message, matcher);
811
});
912

1013
this.pushResult({
1114
result: matchedDeprecations.length !== 0,
1215
actual: matchedDeprecations,
1316
expected: null,
14-
message: 'Expected deprecations during test, but no deprecations were found.'
17+
message:
18+
'Expected deprecations during test, but no deprecations were found.',
1519
});
16-
}
20+
};
1721

1822
if (typeof cb !== 'function') {
19-
test(getDeprecations());
23+
// cb is not a callback, so we assume it is the matcher
24+
test(getDeprecations(), cb);
2025
} else {
21-
const maybeThenable = getDeprecationsForCallback(cb);
22-
if (maybeThenable !== null && typeof maybeThenable === 'object' && typeof maybeThenable.then === 'function') {
23-
return maybeThenable.then(test);
26+
const maybeThenable = getDeprecationsDuringCallback(cb);
27+
if (
28+
maybeThenable !== null &&
29+
typeof maybeThenable === 'object' &&
30+
typeof maybeThenable.then === 'function'
31+
) {
32+
return maybeThenable.then((deprecations) => test(deprecations, matcher));
2433
} else {
25-
test(maybeThenable);
34+
test(maybeThenable, matcher);
2635
}
2736
}
28-
2937
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import checkMatcher from './utils/check-matcher';
2+
import {
3+
getDeprecations,
4+
getDeprecationsDuringCallback,
5+
} from '@ember/test-helpers';
6+
7+
export default function expectNoDeprecation(cb, matcher) {
8+
const test = (deprecations, matcher) => {
9+
const matchedDeprecations = deprecations.filter((deprecation) => {
10+
return checkMatcher(deprecation.message, matcher);
11+
});
12+
13+
this.pushResult({
14+
result: matchedDeprecations.length === 0,
15+
actual: matchedDeprecations,
16+
expected: [],
17+
message:
18+
'Expected no deprecations during test, but deprecations were found.',
19+
});
20+
};
21+
22+
if (typeof cb !== 'function') {
23+
// cb is not a callback, so we assume it is the matcher
24+
test(getDeprecations(), cb);
25+
} else {
26+
const maybeThenable = getDeprecationsDuringCallback(cb);
27+
if (
28+
maybeThenable !== null &&
29+
typeof maybeThenable === 'object' &&
30+
typeof maybeThenable.then === 'function'
31+
) {
32+
return maybeThenable.then((deprecations) => test(deprecations, matcher));
33+
} else {
34+
test(maybeThenable, matcher);
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,65 @@
1-
import { end, _currentRunloop, _hasScheduledTimers, _cancelTimers } from '@ember/runloop';
1+
import {
2+
run,
3+
end,
4+
_getCurrentRunLoop,
5+
_hasScheduledTimers,
6+
_cancelTimers,
7+
} from '@ember/runloop';
28

9+
function getCurrentRunLoop() {
10+
// Ember 3.24.4 does not have _getCurrentRunLoop, but does have run.currentRunLoop;
11+
if ('currentRunLoop' in run) {
12+
return run.currentRunLoop;
13+
} else {
14+
return _getCurrentRunLoop();
15+
}
16+
}
17+
18+
// TODO: It seems very odd to mix runloop + timers into a runloop
19+
// specific assertion.
20+
//
21+
// We should likely:
22+
//
23+
// * have timer specific expectations
24+
// * have runloop specific expectations
25+
// * not have either cancel timers or runloop, rather those should
26+
// be the explicitly choosen by the user
327
export default function expectNoRunloop() {
4-
if (_currentRunLoop) {
28+
if (getCurrentRunLoop()) {
529
this.pushResult({
630
result: false,
7-
actual: run.currentRunLoop,
31+
actual: getCurrentRunLoop(),
832
expected: null,
9-
message: 'Should not be in a run loop at end of test'
33+
message: 'expected no active runloop',
1034
});
1135

12-
while (_currentRunLoop) {
36+
while (getCurrentRunLoop()) {
1337
end();
1438
}
39+
} else {
40+
this.pushResult({
41+
result: true,
42+
actual: null,
43+
expected: null,
44+
message: 'expected no active runloop',
45+
});
1546
}
1647

1748
if (_hasScheduledTimers()) {
1849
this.pushResult({
1950
result: false,
2051
actual: true,
2152
expected: false,
22-
message: 'Ember run should not have scheduled timers at end of test'
53+
message: 'expected no active timers',
2354
});
2455

2556
_cancelTimers();
57+
} else {
58+
this.pushResult({
59+
result: true,
60+
actual: false,
61+
expected: false,
62+
message: 'expected no active timers',
63+
});
2664
}
2765
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getWarnings, getWarningsDuringCallback } from '@ember/test-helpers';
2+
3+
export default function expectNoWarning(callback) {
4+
const warnings =
5+
typeof callback === 'function'
6+
? getWarningsDuringCallback(callback)
7+
: getWarnings();
8+
9+
this.pushResult({
10+
result: warnings.length === 0,
11+
actual: warnings,
12+
expected: [],
13+
message: 'Expected no warnings during test, but warnings were found.',
14+
});
15+
}

addon-test-support/assertions/expect-no-warnings.js

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import checkMatcher from './utils/check-matcher';
2-
import { getWarningsDuringCallback } from '@ember/test-helpers';
2+
import { getWarningsDuringCallback, getWarnings } from '@ember/test-helpers';
33

4-
export default function expectWarnings(callback, matcher) {
4+
export default function expectWarning(callback, matcher) {
55
let warnings;
66
if (typeof callback === 'function') {
77
warnings = getWarningsDuringCallback(callback);
@@ -10,12 +10,15 @@ export default function expectWarnings(callback, matcher) {
1010
warnings = getWarnings();
1111
}
1212

13-
const matchedWarnings = warnings.filter(warning => checkMatcher(warning.message, matcher);
13+
const actual = warnings.filter((warning) =>
14+
checkMatcher(warning.message, matcher)
15+
);
1416

17+
const result = actual.length !== 0;
1518
this.pushResult({
16-
result: matchedWarnings.length !== 0,
17-
actual: matchedWarnings,
19+
result,
20+
actual,
1821
expected: null,
19-
message: 'Expected warnings during test, but no warnings were found.'
22+
message: 'Expected warnings during test, but no warnings were found.',
2023
});
2124
}

addon-test-support/assertions/no-deprecations.js

-11
This file was deleted.

addon-test-support/assertions/utils/check-matcher.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
export function checkMatcher(message, matcher) {
1+
function includes(message, search) {
2+
return message.includes
3+
? message.includes(search)
4+
: message.indexOf(search) !== -1;
5+
}
6+
7+
// TODO: test
8+
export default function checkMatcher(message, matcher) {
29
if (typeof matcher === 'string') {
310
return includes(message, matcher);
411
} else if (matcher instanceof RegExp) {
512
return !!message.match(matcher);
613
} else if (matcher) {
7-
throw new Error(`[ember-qunit] can only match Strings and RegExps. "${typeof matcher}" was provided.`);
14+
throw new Error(
15+
`[ember-qunit] can only match Strings and RegExps. "${typeof matcher}" was provided.`
16+
);
817
}
918

1019
// No matcher always returns true. Makes the code easier elsewhere.

addon-test-support/index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@ import { installTestNotIsolatedHook } from './test-isolation-validation';
2727

2828
let waitForSettled = true;
2929

30-
import deprecationsInclude from './asserts/deprecations-include';
31-
import deprecations from './asserts/deprecations';
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';
36-
37-
export function setup(assert) {
30+
import deprecationsInclude from './assertions/deprecations-include';
31+
import deprecations from './assertions/deprecations';
32+
import expectNoDeprecation from './assertions/expect-no-deprecation';
33+
import expectDeprecation from './assertions/expect-deprecation';
34+
import expectNoRunloop from './assertions/expect-no-runloop';
35+
// import expectWarning from './assertions/expect-warning';
36+
//
37+
export function setupAsserts(assert) {
3838
// TODO: decide which of these we should keep, which depreacte and which drop.
3939
assert.deprecationsInclude = deprecationsInclude;
4040
assert.deprecations = deprecations;
41-
assert.expectNoDeprecations = noDeprecations; // compat
42-
assert.noDeprecations = noDeprecations;
41+
assert.expectNoDeprecation = expectNoDeprecation;
4342
assert.expectDeprecation = expectDeprecation; // compat
4443
assert.expectNoRunloop = expectNoRunloop; // compat but fixed name
4544
// around for compat
@@ -241,5 +240,9 @@ export function start(options = {}) {
241240
startTests();
242241
}
243242

243+
if (options.setupAssertAdditions !== false) {
244+
setupAsserts(QUnit.assert);
245+
}
246+
244247
setupResetOnerror();
245248
}

server/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (app) => app.get('/', (_, res) => res.redirect('/tests'));
+23-11
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,58 @@
11
import { module, test } from 'qunit';
2-
import expectAssertion from 'ember-qunit/assertions/expect-assertion';
2+
// import expectAssertion from 'ember-qunit/assertions/expect-assertion';
33
import { assert as emberAssert } from '@ember/debug';
44

5-
module('expectAssertion', function(hooks) {
5+
module.skip('expectAssertion', function (hooks) {
66
let mockAssert;
77

88
hooks.beforeEach(() => {
99
mockAssert = {
1010
pushedResults: [],
11-
expectAssertion
11+
expectAssertion,
1212
};
1313
});
1414

15-
test('called with assert', function(assert) {
15+
test('called with assert', function (assert) {
1616
mockAssert.expectAssertion(() => {
1717
emberAssert('testing assert');
1818
});
1919

20-
assert.ok(mockAssert.pushedResults[0].result, '`expectAssertion` captured deprecation call');
20+
assert.ok(
21+
mockAssert.pushedResults[0].result,
22+
'`expectAssertion` captured deprecation call'
23+
);
2124
});
2225

23-
test('called without deprecation', function(assert) {
26+
test('called without deprecation', function (assert) {
2427
mockAssert.expectAssertion(() => {
2528
emberAssert('testing assert', true);
2629
});
2730

28-
assert.notOk(mockAssert.pushedResults[0].result, '`expectAssertion` logged failed result');
31+
assert.notOk(
32+
mockAssert.pushedResults[0].result,
33+
'`expectAssertion` logged failed result'
34+
);
2935
});
3036

31-
test('called with deprecation and matched assert', function(assert) {
37+
test('called with deprecation and matched assert', function (assert) {
3238
mockAssert.expectAssertion(() => {
3339
emberAssert('testing assert');
3440
}, /testing/);
3541

36-
assert.ok(mockAssert.pushedResults[0].result, '`expectAssertion` captured deprecation call');
42+
assert.ok(
43+
mockAssert.pushedResults[0].result,
44+
'`expectAssertion` captured deprecation call'
45+
);
3746
});
3847

39-
test('called with deprecation and unmatched assert', function(assert) {
48+
test('called with deprecation and unmatched assert', function (assert) {
4049
mockAssert.expectAssertion(() => {
4150
emberAssert('testing assert');
4251
}, /different/);
4352

44-
assert.notOk(mockAssert.pushedResults[0].result, '`expectAssertion` logged failed result');
53+
assert.notOk(
54+
mockAssert.pushedResults[0].result,
55+
'`expectAssertion` logged failed result'
56+
);
4557
});
4658
});

0 commit comments

Comments
 (0)