Unit testing is something most of us dev’s don’t think much of. Until we encounter some simple to solve bugs or have regressions in code that drives us crazy.
I enjoy Jasmine testing framework right now. For my node projects, I like to use the node package jasmine-node. However, Electron is basically a web browser with node conveniences, so we want to test browser related things.
Since Electron applications take a unique approach to combining elements from the browser with conveniences from node, such as
global and other keywords specific to node, testing gets a little more complicated.
I’m going to outline a few of the approaches I took. I’m sure they are not perfect, I’m still learning and I’m outlining that here.
Tools of the trade
I outlined some things I did to test AngularJS in a previous post. I pretty much use the same tools and set up:
Now I’ve got my
1 2 3 4 5 6 7 8 9
Now we’re set up to do some testing!
require to AngularJS service
I first wanted a one stop shop for all my node conveniences in one angular js service to contain what Electron provides.
Here’s my service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Test set up for Service
Now, hopefully I have all my node conveniences in one place (
Let’s get a simple test up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
If we run this test without anything else, we’ll see immediately a problem:
My approach to this is simple – create a faked out global variable that represents
require and does what you want, such as:
1 2 3 4 5 6 7 8 9 10 11 12 13
Package.json test setup
Let’s define some quick
scripts to run from our package.json to help others run our tests:
1 2 3 4 5
Now when we run our tests, we’ll have the faked out node modules passed back.
This is just one approach to take to setting up some faking out for node modules using Electron, Angular JS, and Jasmine.
Hope this helps.