Skip to content

Commit 41383db

Browse files
committed
Assert: Remove deprecated assert.push()
Deprecated since QUnit 2.1.1. Ref #986.
1 parent 5903a4f commit 41383db

File tree

4 files changed

+77
-71
lines changed

4 files changed

+77
-71
lines changed

docs/api/assert/push.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ title: assert.push()
44
excerpt: Report the result of a custom assertion.
55
groups:
66
- deprecated
7+
- removed
78
redirect_from:
89
- "/config/QUnit.push/"
910
- "/extension/QUnit.push/"
1011
- "/api/extension/QUnit.push/"
1112
version_added: "1.0.0"
1213
version_deprecated: "2.1.1"
14+
version_removed: "unreleased"
1315
---
1416

1517
`push( result, actual, expected, message )`
@@ -33,6 +35,7 @@ To safely report a global error from inside a plugin or other integration layer,
3335

3436
## Changelog
3537

38+
| UNRELEASED | Removed.
3639
| [QUnit 2.1.0](https://github.com/qunitjs/qunit/releases/tag/2.1.0) | Deprecated. Use `assert.pushResult` instead.
3740
| [QUnit 2.0.0](https://github.com/qunitjs/qunit/releases/tag/2.0.0)| Remove `QUnit.push` alias.
3841
| [QUnit 1.15.0](https://github.com/qunitjs/qunit/releases/tag/1.15.0) | Rename `QUnit.push` to `QUnit.assert.push`, with alias.

docs/api/assert/pushResult.md

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,12 @@ Report the result of a custom assertion.
2424

2525
## Examples
2626

27-
If you need to express an expectation that is not abstracted by a built-in QUnit assertion, you can perform your own logic ad-hoc in an expression, and then pass two directly comparable values to [`assert.strictEqual()`](./strictEqual.md), or pass your own representative boolean result to [`assert.true()`](./true.md).
28-
29-
```js
30-
QUnit.test('bad example of remainder', assert => {
31-
const result = 4;
32-
const actual = (result % 3) === 2;
33-
assert.true(actual, 'remainder');
34-
// In case of failure:
35-
// > Actual: false
36-
// > Expected: true
37-
//
38-
// No mention of the actual remainder.
39-
// No mention of the expected value.
40-
});
41-
42-
QUnit.test('good example of remainder', assert => {
43-
const result = 4;
44-
assert.strictEqual(result % 3, 2, 'remainder');
45-
// In case of failure:
46-
// > Actual: 1
47-
// > Expected: 2
48-
});
49-
50-
QUnit.test('bad example of between', assert => {
51-
const actual = 3;
52-
const isBetween = (actual >= 1 && actual <= 10);
53-
assert.true(isBetween, 'result between 1 and 10');
54-
// In case of failure:
55-
// > Actual: false
56-
// > Expected: true
57-
//
58-
// No mention of the actual remainder.
59-
// No mention of the expected value.
60-
// Cannot be expressed in a useful way with strictEqual()
61-
});
62-
```
63-
64-
### Custom assertion
27+
### Create a QUnit assert plugin
6528

6629
With a custom assertion method, you can control how an assertion should be evaluated, separately from how its actual and expected values are described in case of a failure.
6730

31+
This provides more helpful and transparent diagnostic information when test failures are presented. It also lets you avoid duplication and separate concerns between your test requirements and the way specific a generic and re-usable check is implemented.
32+
6833
For example:
6934

7035
```js
@@ -79,11 +44,64 @@ QUnit.assert.between = function (actual, from, to, message) {
7944
});
8045
};
8146

82-
QUnit.test('custom assertion example', assert => {
83-
const result = 3;
47+
QUnit.test('example', assert => {
48+
const result = 42;
8449
assert.between(result, 1, 10, 'result');
85-
// Example of failure if result is out of range
50+
// Example test failure
8651
// > actual: 42
8752
// > expected: between 1 and 10
8853
});
8954
```
55+
56+
### When to create an assertion
57+
58+
If there isn't a built-in QUnit assertion for something that you need to check, you can always freely express it using inline JavaScript within your test. It is recommended to, whenever possible, end your ad-hoc logic with two values that you can pass to [`assert.strictEqual()`](./strictEqual.md), or pass a boolean result to [`assert.true()`](./true.md).
59+
60+
```js
61+
QUnit.test('remainder example [bad]', assert => {
62+
const actual = 4;
63+
64+
const result = (actual % 3) === 2;
65+
assert.true(result);
66+
67+
// Example failure:
68+
// > Actual: false
69+
// > Expected: true
70+
});
71+
72+
QUnit.test('remainder example [good]', assert => {
73+
const actual = 4;
74+
75+
const result = (actual % 3);
76+
assert.strictEqual(result, 2, 'remainder of mod 3');
77+
78+
// Example failure:
79+
// > Message: remainder of mod 3
80+
// > Actual: 1
81+
// > Expected: 2
82+
});
83+
84+
QUnit.test('between example', assert => {
85+
const actual = 42;
86+
87+
const isBetween = actual >= 1 && actual <= 10;
88+
assert.true(isBetween);
89+
90+
// Example failure:
91+
// > Actual: false
92+
// > Expected: true
93+
});
94+
```
95+
96+
Writing a custom expression like this is perfectly fine occasionally. But, if you need to do this a lot, you do take on additional risks and costs over time:
97+
98+
* Risk of subtle bugs or false positives due to logic duplication.
99+
With a plugin, you can write/document/test it once, and then re-use.
100+
* No mention of the actual number.
101+
* No mention of the expected value(s).
102+
* No description of the problem.
103+
* No (useful) diff.
104+
105+
This is likely to increase the cost of debugging, requiring an issue to first be reproduced and stepped-through locally before the failure is understood. You can compensate for this by maintaining a copy of the most important information in the "message" field of your assertions.
106+
107+
When you create an assertion plugin instead, this is automated as part of the "actual" and "expected" values, which you can control separately from the boolean result.

src/assert.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import dump from './dump';
22
import equiv from './equiv';
3-
import Logger from './logger';
43

54
import config from './core/config';
65
import { objectType, objectValues, objectValuesSubset, errorString } from './core/utilities';
@@ -89,20 +88,6 @@ class Assert {
8988
});
9089
}
9190

92-
push (result, actual, expected, message, negative) {
93-
Logger.warn('assert.push is deprecated and will be removed in QUnit 3.0.' +
94-
' Please use assert.pushResult instead. https://qunitjs.com/api/assert/pushResult');
95-
96-
const currentAssert = this instanceof Assert ? this : config.current.assert;
97-
return currentAssert.pushResult({
98-
result,
99-
actual,
100-
expected,
101-
message,
102-
negative
103-
});
104-
}
105-
10691
// Public API to internal test.pushResult()
10792
pushResult (resultInfo) {
10893
// Destructure of resultInfo = { result, actual, expected, message, negative }

test/main/test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,23 @@ QUnit.module('test', function () {
188188
};
189189

190190
QUnit.test('mod2', function (assert) {
191-
assert.mod2(2, 0, '2 % 2 == 0');
192-
assert.mod2(3, 1, '3 % 2 == 1');
193-
});
194-
195-
QUnit.test('testForPush', function (assert) {
196-
QUnit.log(function (detail) {
197-
if (detail.message === 'should be call pushResult') {
198-
/* eslint-disable qunit/no-conditional-assertions */
199-
assert.equal(detail.result, true);
200-
assert.equal(detail.actual, 1);
201-
assert.equal(detail.expected, 1);
202-
assert.equal(detail.message, 'should be call pushResult');
203-
assert.equal(detail.negative, false);
204-
/* eslint-enable */
191+
var detail;
192+
QUnit.log(function (data) {
193+
if (data.message === 'three') {
194+
detail = data;
205195
}
206196
});
207-
assert.testForPush(1, 1, 'should be call pushResult');
197+
198+
assert.mod2(2, 0, 'two');
199+
assert.mod2(3, 1, 'three');
200+
201+
assert.propContains(detail, {
202+
result: true,
203+
actual: 1,
204+
expected: 1,
205+
message: 'three',
206+
negative: false
207+
});
208208
});
209209

210210
QUnit.module('aliases');

0 commit comments

Comments
 (0)