-
Notifications
You must be signed in to change notification settings - Fork 0
Jasmine TDD
#Directory structure and initialization
In the project root directory, run
jasmine init
This creates two directories and a file:
spec/support/jasmine.json
Next, create two files:
touch spec/spec.js
touch app.js
The directory should now look like:
spec/support/jasmine.json
spec/spec.js
app.js
#app.js app.js is an object of keys and methods. The name of the object is module.exports.
module.exports = {
helloWorld: function() {
console.log("Connected!");
return "Hello world.";
},
addOne: function(num) {
return num + 1;
},
upperCASE: function(arr) {
var hold = [];
for (var i = 0; i < arr.length; i++) {
hold.push(arr[i].toUpperCase());
}
return hold;
}
};
The line console.log("Connected!"); will indicate whether spec.js and app.js are hooked up.
#spec.js In spec.js:
var app = require('./../app.js');
describe('Test helloWorld', function() {
it('Hello world test', function() {
expect(app.helloWorld())
.toEqual("Hello world.");
});
});
describe('Test addOne', function() {
it('Adds one to a number', function() {
expect(app.addOne(1))
.toEqual(2);
});
});
var colors = ['red', 'green', 'blue'];
describe('Test upperCASE', function() {
it('Make uppercase', function() {
expect(app.upperCASE(colors))
.toEqual(['RED', 'GREEN', 'BLUE']);
});
});
You can write the test on one line:
expect(app.nameOfTest(inputData)).toEqual(outputData);
However, the nested parentheses can be confusing! Breaking this into two lines makes the parentheses more clear. If you get the error message "undefined is not a function" on this line, then you may have a parentheses nesting problem.
xit comments out the test. Remove the x to run the test.
#Run jasmine Run the tests from the root directory with the command
jasmine