Skip to content

Instantly share code, notes, and snippets.

@Scoutski
Last active April 7, 2016 04:45
Show Gist options
  • Save Scoutski/3a07566cfe7b9ad3f847 to your computer and use it in GitHub Desktop.
Save Scoutski/3a07566cfe7b9ad3f847 to your computer and use it in GitHub Desktop.
Setting up jasmine and node.js

#Instructions to set up node.js and jasmine

Hello there! In order to complete one of the tests created by Ordermentum for development candidates, we will require you to setup an environment with node.js and the testing framework jasmine. This document provides instructions for how to set up the environment and confirm it is working correctly. If at any time you are stuck or unsure of how to proceed, feel free to get in touch with the employee who directed you to this page.

The first step is to set up node by going to the node website and downloading the appropriate version for your operating system.

Follow the instructions until the setup is complete and open a new instance of your terminal window. To check that node has installed, run the following command:

node -v

Assuming everything went correctly, you should be able to see vX.x.x which means that node has now been successfully installed. As some background information, Node can be used to run JavaScript programs in the terminal, as it is used to run server-side code that is written in JavaScript.

One of the components that is installed with node is the node package manager, or NPM for short. NPM is used to installed different libraries for you to use in your code. There are two main ways of installing a package, the first is to install it globally (using a -g flag) or to install it for the current piece of software.

In this case, we want to set up Jasmine for specific use with node. In order to do so, enter the following command:

npm install -g jasmine-node

Similar to before, to check this is installed, run the following command:

jasmine-node --version

Again, assuming everything went well, you should see which version is installed. This means that it is now possible for us to create a node application and then test it using jasmine.

In order to ensure that everything is working, let's set up a very basic test environment so you understand how it works. Start by creating two files in a directory set up for this example: app.js and app.spec.js

Copy this code into the app.spec.js file:

var helloWorld = require("./app");
describe('helloWorld()', function() {
  it('returns the string: "Hello World"', function() {
    expect(helloWorld()).toEqual("Hello World");
  });
});

Just looking at this code quickly, the first line is using node's in built require function to look for a file in the same directory called 'app.js'. The describe and it lines are used to set up the context for the test, to say what we are testing (in this case the helloWorld() function) and what we expect it to do (return the string "Hello World"). The real test happens in the expect line, where the code says, we expect that when we run the function 'helloWorld()' that it will have a return statement that gives us "Hello World" back.

One more important thing that you must know about is 'module.exports'. It probably makes some sense to you now that inside the app.js file, you need to create a function that returns the string "Hello World", there is one problem however. If you were to try create the function like this:

var helloWorld = function()

You'd end up with errors telling you that there isn't a function called helloWorld. The reason is because of the way require() works in node. In order for require to access a piece of code in a different file, that code must be exported using module.exports. This means that your function inside the app.js file can't be called helloWorld, it has to be called module.exports, like so:

module.exports = function() {

At this point, complete the module.exports function to return "Hello World" as a string and when you think you've got it working, make sure your terminal window is in the same directory and run this command to run your test:

jasmine-node app.spec.js

If you recieve a message that finishes with "1 test, 1 assertion, 0 failures, 0 skipped" then everything has worked! Your test has passed and you've successfully created a passing test in jasmine-node. If the test failed, read the error message and try to figure out what caused the problem and try again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment