Generate unit test boilerplate with platelunch

Platelunch is a tool that will generate unit tests based on source files.
I created it because a lot of projects have zero unit tests and this is a quick way to genreate the boilerplate unit
test code.

Currently jest is the only testing framework that is supported. More to come!

CLI

Pass in the directory where the source files are located and unit test files will be generated for every ‘.js’ file that’s
found.

1
platelunch --test-framework jest "src/**/*.js"

I’m passing a glob in the example above but you can have it generate only one unit test file.

1
platelunch --test-framework jest "src/some-file.js"

A directory __tests__ will be created at the root of the project that contain all the unit tests. You’ll have to modify
the unit test files to include any mocking. Only one test per function or class method is created.

Full source code is available on github.

Autogenerate unit test boilerplate. Part 2.

Part 1 deals with parsing a javascript file to get an AST, looking for FunctionDeclarations, MemberExpressions and generating a model. The model will be used to generate a new AST that will represent the boilerplate code for a unit test. This article will deal with generating the boilerplate unit test AST and code.

The source code I want to generate a unit test will be the following:

1
2
3
4
5
6
7
function sayHello(name) {
const greeting = "Hello ";
return greeting + name;
}
module.exports = sayHello;

Read More

Autogenerate unit test boilerplate. Part 1.

Most javascript projects I work on don’t have any unit tests. I only started adding unit tests to my workflow in the past year but have found them extremely useful. Adding unit tests to an existing project can be a pain. Especially some of the boilerplate code for each unit test. I wanted a tool that would perform four tasks:

  1. Parse the javascript source code using babylon to create an Abstract Syntax Tree or AST. Use AST Explorer for a nice visual of what an AST looks like.
  2. Traverse the AST, using babel-traverse, and look for functions that would require a test.
  3. Create a model that stores information on each function that will be tested. This information would include the function name, parameters, any calls to other functions and if it returns a value.
  4. Use the model to genreate a new AST that would be the unit test. I’ll be using babel-generator.

I only wanted to genreate most of the boilerplate code that is needed for a unit test. Things like importing or requiring modules, setting up any mocks, and creating describe/test blocks for any functions.

The test won’t include any expectations. Stuff like this expect(true).toBe(true). I’ll leave that for the developer. It can get challenging to understand the purpose of a function by analyizing the AST. I also didn’t want this tool to remove the developer from writing a unit test.

Read More

Aurelia Testing With Jest

Setting up Jest to test your Aurelia components is pretty easy. The Aurelia team has a great collection of starter kits that can get you setup using Aurelia. I started with the esnext-webpack version and stripped out some dependencies.

The Aurelia team also has a aurelia-testing which I haven’t used. For this example I only used Jest.

Full source code for this example aurelia-testing-jest

Let’s start!

Directory structure

1
2
3
4
5
6
7
8
9
10
11
12
.
|__src
| |__app.js
| |__app.html
| |__main.js
|
|__test
| |__jest-pretest.js
| |__unit
| |__app.spec.js
|
|__package.json

All tests are in test/unit directory and end in .spec.js. For this example I’ll be testing the src/app.js component.

Read More

Default export value in Babel 6.x

In ECMAScript 6 creating a module whose default export is a class would look like the following:

1
2
3
4
5
export default class MyClass {
getMessage() {
return 'hello';
}
}

Using Babel 6.x to transpile this would look like the following:

1
2
3
4
5
6
7
8
9
10
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
// More transpilation stuff goes here.
// This is the important line!
exports.default = MyClass;

Read More