-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial-08-suman-anti-patterns.html
executable file
·359 lines (252 loc) · 15.6 KB
/
tutorial-08-suman-anti-patterns.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Tutorial: Suman Anti-Patterns</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Tutorial: Suman Anti-Patterns</h1>
<section>
<header>
<h2>Suman Anti-Patterns</h2>
</header>
<article>
<p>There are several anti-patterns when using Suman:</p>
<p>[1.] <strong>Anti-pattern number 1.</strong> self/that. I am personally a fan of self/that usage. However, when using Suman, it is indeed an anti-pattern. If you notice that you
have used "self" or "that" instead of "this" to call something from the Suman API, then you should write your code to avoid the self pattern when using Suman. With arrow functions, you can
avoid the self/that pattern, when using functional loops for example. The reason to avoid self/that is that a reference to self/that might show up in a nested describe block and then
you may start registering test cases and hooks to the wrong block. </p>
<p>You should always endeavor to use "this" when calling this.describe/this.before/this.after etc, and you will be all good.</p>
<p>[2.] <strong>Anti-pattern number 2.</strong> Putting code outside of and above <code>Test.describe</code> (the call that creates the root suite). As much code of your test code as possible should be inside the Test.describe callback.
There are several reasons to do this. It makes it a bit easier to see the title of your suite. More importantly, the more setup code before
a Test.describe/Test.suite call, the less code sharing you have => If you have a lot of code above your Test.describe call, it probably means you aren't using the
Suman helper files effectively or correctly, (suman.ioc.js, suman.order.js, suman.hooks.js, etc).</p>
<p>[3.] <strong>Anti-pattern number 3.</strong> Using arrow functions, generator functions, or async/await for describe/suite blocks. Describe blocks are only designed to register callbacks synchronously.
Arrow functions are useful for Suman, and they can be used everywhere except for describe blocks. This has to do with arrow functions binding the context for the callback to the wrong value. Describe blocks are designed to bind the callback to a new value (not the context of the current lexical scope), and to register
all API calls synchronously. Suman is designed to throw an exception if any library call is made after a describe block function has returned.</p>
<blockquote>
<p>in other words don't do this:</p>
</blockquote>
<pre class="prettyprint source"><code>
Test.describe('root test suite', => {
});</code></pre><blockquote>
<p>or this:</p>
</blockquote>
<pre class="prettyprint source"><code>
Test.describe('root test suite', => {
this.describe('uses generator function inappropriately', function *{
});
});</code></pre><p>The reason why arrow functions are not permitted is because we need to bind to a new context in the callback.
Generator functions simply make no sense because the callback is supposed to run synchronously, even though it may be originally
fired asynchronously. </p>
<p>[4.] <strong>Anti-pattern number 4.</strong> Nesting hooks and test cases. Describe blocks (aka child suites) are supposed to be nested! But hooks and test cases are not designed to be nested.
Suman will throw an error if you try to do it, whereas Mocha would let you errantly do it; see this issue:
https://github.com/mochajs/mocha/issues/1975, LOL, sorry Tom, wasn't me.</p>
<p>in other words, don't do this:</p>
<pre class="prettyprint source"><code> this.it('outer', t => {
this.it('inner', t => { // Suman will throw an error if you try to nest a test case within a test case
});
});</code></pre><p> or this:</p>
<pre class="prettyprint source"><code> this.before('outer', t => {
this.beforeEach('outer', t => { // Suman will throw an error if you try to nest a hook within a hook
});
this.it('inner', t => { // Suman will throw an error if you try to nest a test case in a hook, or a hook in a test case
});
});</code></pre><p>note that anti-pattern number 4 relates directly to anti-pattern number 7</p>
<p>[5.] <strong>Anti-pattern number 5.</strong> Unnessarily using process.nextTick or setImmediate in hook / test callbacks</p>
<pre class="prettyprint source lang-js"><code>
// it is not necessary to do this
this.it('not necesary', t => {
var c;
if(c = condition()){
c.doSomethingAsync().then(function(val){
t.done(null,val)
});
}
else{
process.nextTick(t); // no need to wrap in nextTick call
}
});
// this is better
this.it('not necesary', t => {
var c;
if(c = condition()){
c.doSomethingAsync().then(function(val){
t.done(null,val)
});
}
else{
t.done(); // calling t.done in the same tick is just fine, because Suman will ensure it is async behind the scenes
}
});</code></pre><p>[6.] <strong>Anti-pattern number 6.</strong> Perhaps the most important anti-pattern to grok.</p>
<p>You must use t.data and t.value to pass data between beforeEach and afterEach hooks and test cases!</p>
<p>[7.] <strong>Anti-pattern number 7.</strong> Don't register Test.describe() / Test.suite callbacks asynchronous.</p>
<p>For example, this will throww an error and Suman will exit prematurely =></p>
<pre class="prettyprint source"><code>
const suman = require('suman');
const Test = suman.init(module, {
pre: ['make-a-bet'],
post: ['destroyAllPools']
});
Test.describe('@TestsPoolio1', {parallel: true}, function (suite, path, async, assert) {
this.it.cb(t => {
setTimeout(t, 1000);
});
});
process.nextTick(function(){
//we register this block asynchronously
Test.describe('@TestsPoolio2', {parallel: true}, function (suite, path, async, assert) {
this.it.cb(t => {
setTimeout(t, 1000);
});
});
});</code></pre><p>[8.] Failing to return a Promise-returning asynchronous function. When not using callback mode,
the test/hook callback function can act as a Promise provider. If nothing is returned, the test will finish without
being able to properly process any asynchronous behavior.</p>
<p>This following is simply incorrect, the test case will finish in the same tick and we won't be able to capture the Promise</p>
<pre class="prettyprint source"><code>
this.it('throws error', t => {
asyncFn().then(function(){
throw new Error('now this error gets captured correctly');
});
});</code></pre><p>This is correct, you must return the Promise in the test case or hook callback</p>
<pre class="prettyprint source"><code>
this.it('throws error', t => {
return asyncFn().then(function(){ // return the Promise
throw new Error('now this error gets captured correctly');
});
});</code></pre><p>[9.] Suman anti-pattern number 9. Putting delay functions inside before hooks.</p>
<p>It makes sense at first, but you must remember that the delay/resume functionality is completely different than the before/after hook functionality.
before/after hook are related to running before and after test cases.</p>
<p>On the other hand, delay/resume have to do with describe blocks, and delaying the running of their respective callback functions.</p>
<p>As an example, if you only call the delay function from inside the before hook, the delay function will never ever get called,
and a timeout will occur:</p>
<pre class="prettyprint source"><code>const suman = require('suman');
const Test = suman.init(module, {});
Test.describe('@TestsPoolio1', {parallel: true}, function (suite, path, async, assert, delay) {
this.before(t => {
console.log('before'); // we will never get here
delay(); // this will never get called
});
this.it(t => {
console.log('test case that will never be invoked,
because all describe blocks have to run first before hooks and test cases run')
});
this.describe('this block will never be invoked',function () {
// this block will never be invoked, because delay is not called because it is inside the before hook
// which will never get called until we register all describe blocks
console.log('describe');
this.it.cb(t => {
console.log('in test case');
setTimeout(t, 1000);
});
});
});</code></pre><p>instead, this is the right thing to do:</p>
<pre class="prettyprint source"><code>const suman = require('suman');
const Test = suman.init(module, {});
Test.describe('@TestsPoolio1', {parallel: true}, function (suite, path, async, assert, delay) {
//delay will be invoked after 3 seconds, then the nested describe block callback will be fired
setTimeout(delay,3000);
this.before(t => {
console.log('before');
});
this.it('one',t => {
});
this.describe('this block will now be invoked',function () {
// by delaying this callback from firing, we can register a dynamic number of test cases,
// and even a dynamic number of hooks
console.log('describe');
this.it.cb('two', t => {
setTimeout(t, 1000);
});
});
});</code></pre><p>[10.] adding unnessary custom timeouts => use the internal timeout val!</p>
<p>// this is unnecessary:</p>
<pre class="prettyprint source"><code> this.beforeEach.cb('create crucible server', t => {
setTimeout(t.fatal, 6000); // you don't need this, the library has this built-in
const nodeEnv = t.value.NODE_ENV;
const port = t.data.port = nextPort();
const server = t.data.server = child_process.spawn('node', [projectRoot], {
env: Object.assign({}, process.env, {
NODE_ENV: nodeEnv,
port: port
})
});
server.stdout.on('data', function onData(d) {
if (String(d).match(/listening/)) {
server.stdout.removeListener('data', onData);
cb(null);
}
});
server.once('error', t.fatal);
});</code></pre><p>instead, do this:</p>
<pre class="prettyprint source"><code>
this.beforeEach.cb('create crucible server', {timeout: 6000}, t => {
const nodeEnv = t.value.NODE_ENV;
const port = t.data.port = nextPort();
const server = t.data.server = child_process.spawn('node', [projectRoot], {
env: Object.assign({}, process.env, {
NODE_ENV: nodeEnv,
port: port
})
});
server.stdout.on('data', function onData(d) {
if (String(d).match(/listening/)) {
server.stdout.removeListener('data', onData);
cb(null);
}
});
server.once('error', t.fatal);
});</code></pre><p>[11.] Anti-pattern</p>
<pre class="prettyprint source lang-js"><code>
// returning a promise from a callback mode function means any promise related errors will not get caught
it.cb('yes', {timeout: 30000}, t => {
return Client.create(conf).then(c => {
c.lock('z', function (err) {
if (err) return t(err);
c.unlock('z', t);
});
});
});</code></pre><p>[12.] Anti-pattern with promises</p>
<pre class="prettyprint source lang-js"><code>
// - if you implement your own error-handler - the framework won't see the error and the test will pass - a false result
it('works', t => {
return images.upload('https://images-na.ssl-images-amazon.com/eage.jpg')
.then(function(val){
console.log(val);
}, function(err){
console.error(err.stack || err);
});
});
// to fix this, use Promise.reject =>
it('works', t => {
return images.upload('https://images-na.ssl-images-amazon.com/eage.jpg')
.then(function(val){
console.log(val);
}, function(err){
console.error(err.stack || err);
return Promise.reject(err); // <<<<<<<<<<<<<
});
});</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Tutorials</h3><ul><li><a href="tutorial-00-about.html">About Suman</a></li><li><a href="tutorial-01-getting-started.html">Getting Started with Suman</a></li><li><a href="tutorial-02-simple-examples.html">Simple Examples</a></li><li><a href="tutorial-03-high-level-overview.html">Higher Level Overview</a></li><li><a href="tutorial-04-command-line-options.html">Command line options</a></li><li><a href="tutorial-05-advanced-use.html">Advanced Use</a></li><li><a href="tutorial-06-suman-config-file.html">06-suman-config-file</a></li><li><a href="tutorial-07-suman-patterns-and-recipes.html">Suman Patterns and Recipes</a></li><li><a href="tutorial-08-suman-anti-patterns.html">Suman Anti-Patterns</a></li><li><a href="tutorial-09-suman-reporters.html">Suman reporters and creating custom reporters</a></li><li><a href="tutorial-10-dependency-injection.html">10-dependency-injection</a></li><li><a href="tutorial-11-advanced-installation.html">Advanced installation of Suman</a></li><li><a href="tutorial-12-using-suman-with-babel.html">Using Suman with Babel</a></li><li><a href="tutorial-13-test-dir-organization.html">How to organize your test directory</a></li><li><a href="tutorial-14-debugging-suman.html">How to debug Suman tests</a></li><li><a href="tutorial-15-testing-child-processes-with-suman.html">Testing and debugging processes that spawn child processes</a></li><li><a href="tutorial-16-using-spies-with-suman.html">Using test spies with Suman</a></li><li><a href="tutorial-17-suman-server-and-web-reporter.html">About Suman server and built-in web reporter</a></li><li><a href="tutorial-18-tips-and-tricks.html">Tips and tricks</a></li><li><a href="tutorial-19-converting-from-mocha.html">Converting from Mocha to Suman</a></li><li><a href="tutorial-21-running-suman-against-shell-scripts.html">21-running-suman-against-shell-scripts</a></li><li><a href="tutorial-22-case-studies.html">22-case-studies</a></li><li><a href="tutorial-22-programmatic-usage-and-macros.html">22-programmatic-usage-and-macros</a></li><li><a href="tutorial-27-suman-best-practices.html">Best practices with Suman</a></li><li><a href="tutorial-30-anatomy-of-suman-suite.html">Anatomy of Suman Test Suites</a></li><li><a href="tutorial-31-workflows-with-suman.html">Workflows with Suman => Watchers and transpilation</a></li><li><a href="tutorial-40-integrating-with-ci-cd.html">Integrating with CI/CD</a></li><li><a href="tutorial-41-suman-exit-codes.html">List of Suman exit codes</a></li><li><a href="tutorial-42-hidden-secret-features.html">Some secret / hidden features of Suman</a></li><li><a href="tutorial-50-suman-parallelism.html">Complete explanation of Suman parallelism</a></li><li><a href="tutorial-70-usage-with-code-coverage-tools.html">Usage with code coverage tools (namely Istanbul)</a></li><li><a href="tutorial-90-roadmap.html">Suman roadmap and upcoming efforts</a></li><li><a href="tutorial-99-faq.html">FAQ</a></li><li><a href="tutorial-advanced-parallelization-2.html">advanced-parallelization-2</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Wed Feb 08 2017 22:39:23 GMT-0800 (PST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>