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

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

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

Find Stored Procedure Dependencies

Recently working in Microsoft SQL Server I needed to find all the dependencires for a specific stored procedure. I ended up with the following:

1
2
3
4
5
6
use MY_DB
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%myStoredProcedure%'
AND ROUTINE_TYPE='PROCEDURE'

This query looks through all the stored procedure definitions, in the MY_DB database, for the words myStoredProcedure. The results will display all the stored procedures including the myStoredProcedure.