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.”
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.
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:
But in order for Istanbul to pickup this comment, the code needs to look like this:
1
/* istanbul ignore next */functionbabelFunctionHere()...
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:
/** * 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';
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
Download and install babel-plugin-transform-runtime.
Add “plugins”: [“transform-runtime”] to either .babelrc or the babel section of your package.json file
Download and install babel-jest
Modify babel-jest/src/index.js as indicated above.
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.
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.
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.
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.
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.
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.
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.
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.