You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/lifecycle.md
+76-42Lines changed: 76 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,13 +115,14 @@ QUnit.module('Foo', function (hooks) {
115
115
### Example: Async hook callback
116
116
117
117
```js
118
-
QUnit.module('Database connection', function (hooks) {
118
+
QUnit.module('Database', function (hooks) {
119
119
hooks.before(asyncfunction () {
120
-
awaitMyDb.connect();
120
+
this.conn=MyDb.createConnection();
121
+
awaitthis.conn.open();
121
122
});
122
123
123
124
hooks.after(asyncfunction () {
124
-
awaitMyDb.disconnect();
125
+
awaitthis.conn.close();
125
126
});
126
127
127
128
// define tests...
@@ -131,32 +132,59 @@ QUnit.module('Database connection', function (hooks) {
131
132
You can also define async hooks without modern async-await syntax, by returning a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or other "then"-able object:
132
133
133
134
```js
134
-
QUnit.module('Database connection', {
135
+
QUnit.module('Database', {
135
136
before:function () {
136
-
returnnewPromise(function (resolve, reject) {
137
-
MyDb.connect(function (err) {
138
-
if (err) {
139
-
reject(err);
140
-
} else {
141
-
resolve();
142
-
}
143
-
});
144
-
});
137
+
this.conn=MyDb.createConnection();
138
+
returnthis.conn.open();
145
139
},
146
140
after:function () {
147
-
returnnewPromise(function (resolve, reject) {
148
-
MyDb.disconnect(function (err) {
149
-
if (err) {
150
-
reject(err);
151
-
} else {
152
-
resolve();
153
-
}
154
-
});
155
-
});
141
+
returnthis.conn.close();
156
142
}
157
143
});
158
144
```
159
145
146
+
### Example: Sharing hooks
147
+
148
+
If you have multiple components that extend or call the same feature, you may find yourself wanting to reuse a set of mocks, test fixtures, or utility functions across different QUnit modules.
149
+
150
+
You can do this by providing a set of re-usable hooks, which you can effectively "merge" into your module.
151
+
152
+
If your setup only needs to run once, consider placing it in a [bootstrap script](./api/config/index.md) instead, along any other configuration.
153
+
154
+
If your setup should run for all test modules, use a global [`QUnit.hooks`](./api/QUnit/hooks.md) which applies automatically without any per-module setup.
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 the [`QUnit.test()`](./api/QUnit/test.md) function and hook callbacks.
@@ -167,8 +195,12 @@ The `before` hook has access to the base object that tests and child modules inh
@@ -186,10 +218,12 @@ QUnit.module('Maker', function (hooks) {
186
218
If you use arrow functions, beware that the `this` test context is not reachable from inside an arrow function. JavaScript does have a built-in lexical scope that you can use in a similar way. This works the same as a text context for simple cases (i.e. you assign all variables together in `beforeEach`).
187
219
188
220
```js
189
-
QUnit.module('Machine Maker', function (hooks) {
221
+
QUnit.module('Maker', function (hooks) {
222
+
constinventory='ABCDEFG';
223
+
190
224
let parts;
191
225
hooks.beforeEach(function () {
192
-
parts = ['A', 'B'];
226
+
parts = [inventory[0], inventory[1]];
193
227
});
194
228
195
229
QUnit.test('make alphabet', function (assert) {
@@ -236,30 +270,30 @@ QUnit.test('make good music', function (assert) {
0 commit comments