Skip to content

Commit b78eb8a

Browse files
committed
Docs: Fix grammar, shorten alt, improve example on lifecycle page
1 parent 3a7022e commit b78eb8a

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

docs/lifecycle.md

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ The execution order of module hooks in QUnit.
1515
</p>
1616

1717
<figure>
18-
<img src="/resources/qunit-lifecycle-hooks-order.svg" width="676" height="901" alt="" title="Imagine a test suite that uses global hooks,
19-
and has a module called Parent that uses hooks and contains one test called Foo,
20-
and a nested module called Child that also uses hooks and contains one test called Bar.
21-
The execution order is as follows:
22-
1. The Parent module runs its before hook exactly once.
23-
2. Every test in the Parent module inherits the test context from this before hook.
24-
3. This repeats for every test in the Parent module: call global beforeEach, parent beforeEach, actual test (Foo), parent afterEach, and lastly the global afterEach.
25-
4. The Child module inherits context from the Parent's before hook, and runs its own before hook exactly once.
26-
5. Every test in the Child module inherits test context from this before hook.
27-
6. This repeats for every test in the Child module: call the global beforeEach, parent beforeEach, child beforeEach, actual test (Bar), child afterEach, parent afterEach, and lastly the global afterEach.
18+
<img src="/resources/qunit-lifecycle-hooks-order.svg" width="676" height="901" alt="" title="Imagine a test suite with global hooks, and a Parent and Child module that use hooks also. The execution order is:
19+
1. Parent module runs the before hook.
20+
2. Every test in the Parent module inherits context from the before hook, and repeats as follows: call global beforeEach, parent beforeEach, the actual test, parent afterEach, and lastly the global afterEach.
21+
3. The Child module inherits context from the Parent before hook, and then runs its own before hook.
22+
4. Every test in the Child module inherits context from this before hook, and repeats as follows: call global beforeEach, parent beforeEach, child beforeEach, the actual test, child afterEach, parent afterEach, and lastly the global afterEach.
2823
">
2924
</figure>
25+
3026
## Module hooks
3127

3228
_See also: [QUnit.module § Hooks](./api/QUnit/module.md#hooks)_
@@ -38,7 +34,7 @@ You can define the following hooks via `QUnit.module()`:
3834
* `afterEach`: Add a callback after every test.
3935
* `after`: Add a callback after the last test, once per module.
4036

41-
Hooks that run _before_ a test, are executed in the order that they are added (from outer-most to inner-most). This means that utilities and fixtures provided global hooks are safe to use during all tests, and also during the beforeEach hooks of all modules. Likewise, the same is true between a parent module and its child modules.
37+
Hooks that run _before_ a test, are executed in the order that they are added (from outer-most to inner-most). This means that utilities and fixtures provided by global hooks are safe to use during all tests, and also during the beforeEach hooks of all modules. Likewise, the same is true between a parent module and its child modules.
4238

4339
For example:
4440

@@ -105,7 +101,7 @@ QUnit.module('Foo', function (hooks) {
105101
c = 'Stranger';
106102
});
107103

108-
QUnit.test('nested example', (assert) => {
104+
QUnit.test('nested example', function (assert) {
109105
assert.equal(a, 'Hello to this');
110106
assert.equal(b, 'world');
111107
assert.equal(MyApp.greeting(), 'Hello to this world');
@@ -163,7 +159,7 @@ QUnit.module('Database connection', {
163159

164160
## Test context
165161

166-
Each test starts with a fresh copy of the test context as provided by the module. This is generally an empty object. The test context is available as `this` inside any [`QUnit.test()`](./api/QUnit/test.md) function or hook callbacks.
162+
Each test starts with a fresh copy of the test context as provided by the module. This is generally an empty object. The test context is available as `this` inside any [`QUnit.test()`](./api/QUnit/test.md) function or hook callback.
167163

168164
At the end of every test, changes to the test context are automatically reset (e.g. changes by tests or hooks). The next test will start with a fresh context provided by the module.
169165

@@ -210,30 +206,60 @@ QUnit.module('Machine Maker', function (hooks) {
210206

211207
### Example: Set context via options
212208

213-
The following are equivalent:
214-
215209
```js
216210
QUnit.module('example', {
217-
myparts: ['A', 'B']
211+
inventory: 'ABCDEFG',
212+
makeParts (a, b) {
213+
return [this.inventory[a], this.inventory[b]];
214+
},
215+
beforeEach () {
216+
this.parts = this.makeParts(0, 1);
217+
}
218218
});
219219

220220
QUnit.test('make alphabet', function (assert) {
221221
this.parts.push('C');
222222
assert.equal(this.parts.join(''), 'ABC');
223223
});
224+
225+
QUnit.test('make music', function (assert) {
226+
this.parts.push('B', 'A');
227+
assert.equal(this.parts.join(''), 'ABBA');
228+
});
229+
230+
QUnit.test('make good music', function (assert) {
231+
var x = this.makeParts(1, 1).join('');
232+
assert.equal(x, 'BB', 'The King of the Blues');
233+
});
224234
```
225235

226-
Is essentially equivalent to:
236+
This is functionally equivalent to:
227237

228238
```js
229239
QUnit.module('example', {
240+
before () {
241+
this.inventory = 'ABCDEFG';
242+
this.makeParts = function (a, b) {
243+
return [this.inventory[a], this.inventory[b]];
244+
};
245+
},
230246
beforeEach () {
231-
this.myparts = ['A', 'B'];
247+
this.parts = this.makeParts(0, 1);
232248
}
233249
});
234250

235251
QUnit.test('make alphabet', function (assert) {
236252
this.parts.push('C');
237253
assert.equal(this.parts.join(''), 'ABC');
238254
});
239-
```
255+
256+
QUnit.test('make music', function (assert) {
257+
this.parts.push('B', 'A');
258+
assert.equal(this.parts.join(''), 'ABBA');
259+
});
260+
261+
QUnit.test('make good music', function (assert) {
262+
var x = this.makeParts(1, 1).join('');
263+
assert.equal(x, 'BB', 'The King of the Blues');
264+
});
265+
```

0 commit comments

Comments
 (0)