Dave's Notebook

Are You Average or Awesome? 9 Ways to Improve.

The story goes that there were two men, Joe and Frank, who were camping out in the woods when a bear showed up in the camp.  Terrified, they decided the best they could do would be to stay perfectly still until the bear left.  Hopefully, the bear wouldn’t notice them.  As the bear was poking around, Joe says to Frank, “What are we going to do if this doesn’t work?”  Frank says, “Run!”  Joe says, “You really think we can out run a bear?”  Frank says, “I don’t need to out run the bear.  I only need to out run you.”

![](/uploads/2016/05/image.png "9 Ways to Improve")
Photo credit: [Internet Archive Book Images](//www.flickr.com/photos/internetarchivebookimages/14767148885/) via [VisualHunt.com](//visualhunt.com) / [No known copyright restrictions](//flickr.com/commons/usage/)

Read More

ES2015 Code Coverage and Jest (React JS Unit Testing)

As I’ve mentioned before, I’m in the middle of putting together a React reference app and I’m doing it using Test Driven Development.  The problem is, the standard tools for implementing ES2015 code coverage with Jest make it hard to see at a glance if you have 100% code coverage or not because of some issues with the way Jest tells Babel to do the transformations by default, the way Babel transforms the code and implements the auxiliaryCommentBefore option and the way that Istanbul parses the ignore next comments.

I’ve been working on solving this problem for the last month and a half off and on.  I’ve even posted a question about this on Stack Overflow, so I’m pretty sure no one else has a solution for this yet.  I’m not going to say my solution is the best way to solve this problem, but it is a solution, which is better than what we have so far.

ES2015 Code Coverage and Jest

Diagnostics

By default, when Babel transforms your code, it inserts additional functions into the code that it can call to replace the code you wrote that does not yet conform to the syntax you’ve used.  This code gets inserted at the top of the file and shows up in your code coverage reports as several conditions that didn’t get fired.  Yes, it inserts code it never uses because the functions have to work under a variety of scenarios.

For those who are interested in how I figured this out.  The transform results are located in node_modules/jest-cli/.haste_cache.

ES2015 Code Coverage Fix One

OK, so the standard recommended fix for something like this is to place

1
/* istanbul ignore next */

Prior to those functions.  And it just so happens that both Jest and Babel provide a mechanism for adding this comment by using the auxiliaryCommentBefore option.

Only there are two problems with this.

Problem One

If you just set the property like this:

auxiliaryCommentBefore: ‘istanbul ignore next’

Your code will get transformed so that any functions added by Babel will end up looking like this:

1
/*istanbul ignore next*/function babelFunctionHere()...

But in order for Istanbul to pickup this comment, the code needs to look like this:

1
/* istanbul ignore next */ function babelFunctionHere()...

While getting the spaces on either side of ‘istanbul ignore next’ is a simple matter, we have no real control over the space that is necessary between the comment marker and the function keyword.

Problem Two

The second problem with this “fix” is that even if modify the Babel code so that the comment gets inserted correctly, it doesn’t get inserted before EVERY function that Babel inserts.  If it inserts a group of functions, which it does regularly in my code, it only inserts the comment before the first function.

ES2015 Code Coverage Fix Two

What if we didn’t insert the functions in our code?  Well, it just so happens that we can do that relatively easily.

There is a plug-in for Babel called ‘transform-runtime’.  What this plug-in does is that it requires in the functions rather that pasting them into your code.  This way, the functions don’t exist in your code so Istanbul never sees the function block.  Pretty cool.

You can add this to either your .babelrc file or the Babel section of your package.json file by adding a “plugins” section

1
"plugins": ["transform-runtime"]

along with the “presets” section you should already have.

Remaining Issue

While using transform-runtime takes care of most of the issues, there are two functions that still don’t get covered.  In fact, when you look at the transform-runtime code, you find that they are explicitly excluded and if you include them, your code won’t transpile at all.

The good news is, it is only two functions and they both show up as

1
function _interop...

If we can get a hold of the code as it is being transformed, we should be able to do a search and replace to get the correct ‘istanbul ignore next’ string in place prior to the functions.

Well, it just so happens that Jest has the ability to do exactly that.

ES2015 Code Coverage Final Fix

I’m assuming you’ve already installed babel-jest, but just in case, if you have not, install it now.  Install it using –save-dev because we are going to want to be able to modify the code.

Quick fix:

The proper way to fix this would be to write your own version of babel-jest.  But we are going for a quick fix.  Maybe we can get Facebook to implement the changes from this post.  Meanwhile, here is what you want to do.

Locate the src/index.js file in the node_modules/babel-jest directory.  At the time of this writing, the current version looks like this:

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
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const babel = require('babel-core');
const jestPreset = require('babel-preset-jest');

module.exports = {
process(src, filename) {
if (babel.util.canCompile(filename)) {
return babel.transform(src, {
auxiliaryCommentBefore: ' istanbul ignore next ',
filename,
presets: [jestPreset],
retainLines: true,
}).code;
}
return src;
},
};

The first change that you want to make here is to comment out the auxiliaryCommentBefore line.  We no longer need that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

'use strict';

const babel = require('babel-core');
const jestPreset = require('babel-preset-jest');

module.exports = {
process(src, filename) {
if (babel.util.canCompile(filename)) {
return babel.transform(src, {
// auxiliaryCommentBefore: ' istanbul ignore next ',
filename,
presets: [jestPreset],
retainLines: true,
}).code;
}
return src;
},
};

You will notice that what gets returned is the resulting transform of the code.  We want to execute a search and replace on the transformed code.  So, instead of

1
2
3
4
5
6
return babel.transform(src, {
auxiliaryCommentBefore: ' istanbul ignore next ',
filename,
presets: [jestPreset],
retainLines: true,
}).code;

What we want want to do is:

1
2
3
4
5
6
7
8
return babel.transform(src, {
//auxiliaryCommentBefore: ' istanbul ignore next ',
filename,
presets: [jestPreset],
retainLines: true,
}).code
.replace(/function\s_interop/g,
' /* istanbul ignore next */ function _interop');

ES2015 Code Coverage With Jest - Summary

  1. Download and install babel-plugin-transform-runtime.
  2. Add “plugins”: [“transform-runtime”] to either .babelrc or the babel section of your package.json file
  3. Download and install babel-jest
  4. Modify babel-jest/src/index.js as indicated above.

JavaScript Types Nuance

I was once teaching a class on JavaScript to a group of C# developers when someone asked a very logical question, “Are JavaScript Types all derived from Object?” I loved teaching this particular group because they were actively engaged in the material.  So many times, when I teach, the students simply absorb what I say, but they don’t interact with it.  They never ask the question, “What are the implications of what is being said.” My initial instinct was to say ‘no’ based on my experience with the language.  But then as I thought about it later, I thought, “But when I use the debugger on what seems to be a primitive, don’t I see it as an object?”  And as it turns out, my instinct was right.  Not everything in JavaScript is an object.  Although there is quite a bit that you wouldn’t think was an object that is.

Now that we’ve covered JavaScript Objects and JavaScript Object Fields, it is time to move on to the specifics of JavaScript types.

So, why is it, when I look at some primitive values, I see them as objects?  And which types are objects and which are primitives?

![](/uploads/2016/04/image-4.png "image") Photo via [Visualhunt](//visualhunt.com/)

Read More

Do This To Increase Your Client Side Web Development Speed

Over the last year, in particular, I’ve developed a technique for developing the client side of a web application using JavaScript, HTML and CSS that has significantly improved my development speed.  Once I tell you, it will be obvious.  At least, it is to me … now.  But as obvious as it is, I rarely see this technique used on any of the applications my peers are working on.

And while this technique is an outgrowth of my involvement with TDD, this particular technique can be used with or without TDD.

![](/uploads/2016/04/image-3.png "Do This To Increase Your Client Side Web Development Speed")
Photo credit: [JD Hancock](//www.flickr.com/photos/jdhancock/4367347854/) via [VisualHunt](//visualhunt.com) / [CC BY](//creativecommons.org/licenses/by/2.0/)

Read More

JavaScript MVVM - You’re (Probably) Doing it Wrong

If you are using one of the many frameworks that say they are using JavaScript MVVM, you might not be using it the way it should be used. Many of my clients aren’t.

This article will attempt to answer three questions.

  • What is MVVM?
  • What are the advantages of MVVM?
  • What are good MVVM coding practices?
![](/uploads/2016/04/image-1.png "JavaScript & MVVM")
Photo credit: [uka0310](//www.flickr.com/photos/uka0310/8038385310/) via [VisualHunt](//visualhunt.com) / [CC BY](//creativecommons.org/licenses/by/2.0/)

Read More

Ext JS 6 by Sencha - The Good, The Bad, The Ugly

Long time readers may remember that I started using Ext JS about 3 years ago.  At the time, I was using version 4.2.2.  I recently started a new contract where they are using Ext JS 6.0.1.  I have to say, this version solves a lot of the architectural issues I had with the 4.x series.  But, there are still problems.

Since I’ve provided an evaluation of Angular 2 and React JS, I thought providing an evaluation of the current version of Ext JS would be appropriate since these three seem to be the main players in the corporate world.

![](/uploads/2016/04/image.png "Ext JS by Sencha - The Good, The Bad, The Ugly")
Photo credit: [sanbeiji](//www.flickr.com/photos/sanbeiji/5606497634/) via [Visual Hunt](//visualhunt.com) / [CC BY-SA](//creativecommons.org/licenses/by-sa/2.0/)

Read More

An Explanation of the Flux Pattern

Over the last couple of weeks, I’ve mentioned that I’ve been learning React JS.  First in the article “Reaction to React JS and Associated Bits” and then last week in my article “Test Driven Learning”. 

In the first article, I mentioned that if you use React JS, you’ll probably end up using the Flux design pattern and since there are multiple ways of implementing flux, getting a clear definition of what it is and how it should work can be confusing.  At least, I found it confusing.

And now that I’ve figured it out, I thought it might be helpful both to myself and to the programming community at large if I offered my own Explanation of the Flux Pattern.  At the very least, it will give me one more way of solidifying the concept in my own brain.  Maybe it will be helpful to you as well.

![](/uploads/2016/03/image-5.png "Flux")
Photo credit: [jeffreagan](//www.flickr.com/photos/jeffreagan/22033828931/) via [VisualHunt.com](//visualhunt.com) / [CC BY](//creativecommons.org/licenses/by/2.0/)

Read More

Test Driven Learning - An Experiment

As I mentioned last week, I’ve been learning React JS over the last month or so. Up until the start of this project, I would learn a new framework, and then I would try to paste in Test Driven Development after the fact.  I would use the excuse that because I didn’t know the framework well enough, I wouldn’t be able to properly write tests for it.

But this time, I decided to do something different.  What if I wrote tests for my demo application as I was learning this new framework?  My reasoning was that learning how to test code written in the framework was just as important as learning the framework.

What follows are the lessons I learned from this wildly successful experiment.

image Photo credit: twm1340 via Visualhunt.com / CC BY-SA

Read More

Reactions to React JS and Associated Bits

I’ve been learning React JS over the last several weeks.  Currently, I now know 4 of the major JavaScript frameworks: Angular 1, Angular 2, EXTjs (4.2 – 6.0.1), and now React JS.  To be clear, I also know Knockout and JQuery.  But I don’t consider these frameworks so much as libraries.  They’ve helped me understand the principles used in the frameworks, but they are not frameworks.  What follows is a summary of what I consider React’s strengths and weaknesses.

React JS Photo credit: kristin osier via VisualHunt / CC BY

Read More

JavaScript Object Fields

Last week as I was discussing the basics of JavaScript Objects, I kept referring to the members of the object as “fields.”  Never did I call them properties or methods.  This is because all members that are hanging off of an object are treated the same, from a membership perspective.  It is the type of data it contains that makes it behave as what we would normally refer to as a property or a method.

This is an important distinction.

In a strongly typed system, we can say that a member of our object is a property or method simply because it was defined as one or the other when we defined our class. In JavaScript we have neither classes where we can define what something is, nor strong typing.

Read More