The source code I want to generate a unit test will be the following:
The model that is generated from the source code will look like the following:
I’ll be using two modules to create the unit test AST and code:
I want to create a test block for each function in the model. From the source code above I want the test to look like the following:
Note line 3 there’s a variable for the function parameter name. I do this so the developer can assign values to each function parameter. Every function parameter will have a variable defined this way.
First I have to iterate over all the functions in the model. Then I can create a test block for each function. The code below will create an array of test blocks. A test block will be for every function in the model. I’m using the reduce method to iterate over the functions. Inside the reduce method I will
- Create an array of function parameters.
- Create an array of variables There will be one variable for every function parameter.
- Create the function call.
- Create the test block.
Once iterating is completed I’ll add the test block array to a description block.
All function parameters will be defined as an Identifier. Below I’m creating an array of function parameters that’s iterating over the params array from the model.
Every function parameter will have a variable defined within the test block. The variable will have the same name as the parameter and assigned a null value. This will also loop over the params array from the model. Below I’m creating an array of variables.
The function call is a CallExpression in AST terms. The call will contain the function name and parameters. The function also returns a value. I’ll create a VariableDeclaration, named result, that holds the return value of the function call, line 6.
Line 9 contains the functionParameters array the was created above.
Now I can create the test block. It’s an ExpressionStatement containing a CallExpression containing a ArrowFunctionExpression.
The ArrowFunctionExpression will have the variables and the function call. The array, testBlockStatement, line 1, is any array of the variables and the function call that wlll be used in the ArrowFunctionExpression in line 9.
I’ll wrap the all the test blocks within a describe block. The describe block is also a ExpressionStatement containing a CallExpression containing a ArrowFunctionExpression. The ArrowFunctionExpression will have the all the test blocks.
Line 8 refers to the testBlockArray created above.
Finally we can generate a unit test.
Line 3 creates a Program AST node that contains the variable describeBlock from the code above. Line 4 uses the babel-generator module to generate the unit test code. I’m passing an empty object of options to babel-generator. The output variable will be an object having a property code which is the unit test code. I’m only ouputing the code to the console but you can save this to a file.
Next I’ll explore FunctionDeclarations that contain a call to another module and mocking/spying that call.
Source code for this this example is at github.