Now, I’m usually not a strict test driven developer, but these cloud code modules are a different story; the code is running on a server and accessing a 3rd party API. In this situation, quick build/test/debug cycles are impossible and testing is the only way go.
Setting up the test environment
I used Mocha.js for the testing framework and Expect.js for assertions. Mocha supports any assertion framework you can throw at it.
Expect seems to work well and doesn’t generate
Lint warnings like
Should.js. Run the following commands from your project directory.
$ npm install mocha $ npm install expect.js
These commands install the mocha and expect frameworks in the local
node_modules directory. This greatly simplifies your environment. Code can be shared between collaborators without worrying about what framework version each dev has installed.
At this point, your Parse directory should look like this:
cloud / main.js config / global.json node_modules / .bin / mocha / expect /
Creating Cloud Code Modules
We’re all set to test, but first we need something to test. We’re going to add some code that just returns a helpful message. Modify
main.js to look like so:
1 2 3 4 5
What I’ve done is load each Cloud Code function from a separate module, this worked well for me because each function is independent of all other functions, but YMMV. Let’s setup some code to test. First, create a file named
message.js in your
cloud directory. Now modify it to look like so.
1 2 3
A bit about how
Node.js loads modules, since this was all new to me. When each module is loaded, there is an object created called
exports that is available within that module. Any function attached to that object will be available externally when your module is loaded. So in this example, our
message module creates one function called
getMessage. When we call
var message = require('cloud/message.js'); the object returned from the
require() function is that
Now let’s create some tests. First, the default directory for
Mocha tests is
test, so let’s put our test there. Create the test directory and put a file in there named, “messageTests.js”. Modify it to look like this:
1 2 3 4 5 6 7 8 9 10
First we import our
expect modules. The
describe() function is used to define the scope for associated tests. The
it() function defines a Mocha test. Let’s run our test.
Notice that the error message says, “Message response should return the correct message:”, so structure your
it messages to make that message nice and readable. Let’s replace our failing test with an actual tests.
1 2 3 4 5 6 7 8
There’s a few things to notice here. First, the
done() function is an optional parameter to the
it() methods. Any tests within will not complete successfully until the
done() function is called, so you can use it to test callbacks and ensure that they were called. Second, notice the format of the
expect() function. You can read the documentation for more details, but in general, the tests always follow this form, beginning with an
expect() function and ending with a function.
Run the tests again and success! All our tests pass. All in all, it took me less than a day to try out different testing and exception modules, settle on