I’m writing to introduce a small project I’ve just put together. I call it Myrtle (which I guess is some sort of play on words on the Mock Turtle), but in any case, it is a Javascript Mocking Framework!
The project code is available on Github, or you can just grab Myrtle.js if you like.
So anyway, let’s look at what it does. Let’s say you’re writing some tests for your code, and you want to check that a particular function is called. For example, you might have a Validator class which should be called when a form input is changed. Here’s the signature of a basic validation class:
1 2 3 4 5 6 7 8 9 10 |
|
The first step is to spy on the validate method:
1
|
|
Spying on a method modifies it so that metadata about that method is stored with each call to it, without changing the actual functionality. You can access this metadata through the API object returned by Myrtle.spy
.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
What’s important to remember here is that the validate method is still executed as if nothing ever happened. Myrtle wouldn’t be a good spy otherwise…
Other times you want to actually stop functions from running. For example, in a test environment, you might want to suppress error messages which you’re triggering on purpose, or to stop AJAX functions from executing. In these cases, you can stub out a function.
1
|
|
This replaces the displayError
method with a function which does nothing. Care should be taken in these cases that other code isn’t expecting a return value from functions which you stub out completely.
You can also replace a function using stub. This is useful if you want to simulate a method, but not actually execute it – for example, when an AJAX method is called, you can put together a fake response.
1 2 3 |
|
You’ll see there that the first parameter passed to the stub function is the original method. This is useful in cases where you may want to only stub it out based on some criteria, or to modify arguments or return values of the original method.
1 2 3 4 5 6 7 8 9 |
|
The last feature currently in Myrtle is some basic speed profiling. If you want to find out how fast your function is executed, or if it runs slower given some input, you can turn on profiling and find out.
1 2 3 4 5 6 7 8 |
|
Those are the three main features of Myrtle (so far). However, the nice thing about them is that they can all be combined in any way. You can spy on and stub out a method, or spy and profile. Stubbing and profiling probably isn’t so useful though, since you’d only be measuring the speed of performing your replacement method.
There are a few ways to combine the options, and it’s important to know that each of the Myrtle functions returns the exact same API object per function. To demonstrate:
1 2 3 |
|
There’s also the Myrtle
function itself which can be used:
1 2 3 4 5 6 7 |
|
Of course, the last thing to cover is an important one: tidying up. Chances are that you don’t want to stub out your methods for all the tests, and you want to restore them to how they were before at some point. Myrtle makes this super easy.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
So, like I said, the code is on Github. Go have look and let me know what you think!