Skip to content

Commit 81db1c6

Browse files
committed
Docs: Improve rejects() and extend() examples, upgrade guide fix
* Fix bug in upgrade guide where I mistakenly started using arrow functions in an example that involves text context. Happened in commit qunitjs/qunitjs.com@6ab0198592. * Improve rejects() example by simplifying the code and spelling out what would happen in a way that's easy to try out and experience yourself. * Clarify wording in extend() example.
1 parent b78eb8a commit 81db1c6

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

docs/api/assert/async.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ The `count` parameter can be used to require multiple calls to the same callback
7575

7676
```js
7777
function uploadBatch (batch, notify, complete) {
78-
batch.forEach((item) => {
78+
for (const item of batch) {
7979
// Do something with item
8080
notify();
81-
});
81+
}
8282
complete(null);
8383
}
8484

docs/api/assert/rejects.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,23 @@ QUnit.test('rejects example', assert => {
9494
The `assert.rejects()` method returns a `Promise` which handles the (often asynchronous) resolution and rejection logic for test successes and failures. It is not required to `await` the returned value, since QUnit internally handles the async control for you and waits for a settled state. However, if your test code requires a consistent and more isolated state between `rejects` calls, then this should be explicitly awaited to hold back the next statements.
9595

9696
```js
97-
QUnit.test('stateful rejects example', async assert => {
97+
QUnit.test('stateful example', async function (assert) {
9898
let value;
9999

100-
// asynchronously resolve if value < 5, and reject otherwise
101-
function asyncChecker () {
102-
return new Promise((resolve, reject) => {
103-
setTimeout(() => {
104-
if (value < 5) {
105-
resolve();
106-
} else {
107-
reject('bad value: ' + value);
108-
}
109-
}, 10);
110-
});
100+
async function feedMe () {
101+
await Promise.resolve();
102+
if (value === 1) {
103+
throw new Error('I want more');
104+
}
105+
throw new Error('Thank you');
111106
}
112107

113-
value = 8;
114-
await assert.rejects(asyncChecker(), /bad value: 8/);
108+
value = 5;
109+
// if we don't await, then this test will fail,
110+
// because `value = 1` would run too soon
111+
await assert.rejects(feedMe(), /Thank you/);
115112

116-
// if the above was not awaited, then the next line would change the value
117-
// before the previous assertion could occur, and would cause a test failure
118-
value = Infinity;
119-
await assert.rejects(asyncChecker(), /bad value: Infinity/);
113+
value = 1;
114+
await assert.rejects(feedMe(), /I want more/);
120115
});
121116
```

docs/api/extension/QUnit.extend.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ This method will modify the `target` object to contain the "own" properties defi
2828
Use `QUnit.extend` to merge two objects.
2929

3030
```js
31-
QUnit.test('QUnit.extend', assert => {
31+
QUnit.test('using extend', function (assert) {
3232
const base = {
3333
a: 1,
3434
b: 2,
35-
z: 3
35+
zed: 3
3636
};
3737
QUnit.extend(base, {
3838
b: 2.5,
3939
c: 3,
40-
z: undefined
40+
zed: undefined
4141
});
4242

43-
assert.strictEqual(base.a, 1, 'Unspecified values are not modified');
44-
assert.strictEqual(base.b, 2.5, 'Existing values are updated');
45-
assert.strictEqual(base.c, 3, 'New values are defined');
46-
assert.false('z' in base, 'Values specified as `undefined` are removed');
43+
assert.strictEqual(base.a, 1, 'Unspecified keys remain unchanged');
44+
assert.strictEqual(base.b, 2.5, 'Matching keys are replaced');
45+
assert.strictEqual(base.c, 3, 'New keys are added');
46+
assert.false('zed' in base, 'Keys with the value `undefined` are removed');
4747
});
4848
```

docs/upgrade-guide-2.x.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ After:
134134

135135
```js
136136
QUnit.module('router', {
137-
beforeEach: () => {
137+
beforeEach () {
138138
this.router = new Router();
139139
},
140-
afterEach: () => {
140+
afterEach () {
141141
this.router.destroy();
142142
}
143143
});

0 commit comments

Comments
 (0)