Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use jasmine.js to test for console output?

I'm working through the text: Professional JavaScript for Web Developers by Nicholas Zakas and I'm testing the examples with Jasmine.js.

I can currently test the output of a function by specifying a return a value, but I'm running into trouble when there are multiple pieces of data that I want to return.

The textbook uses the alert() method, but this is cumbersome and I don't know how to test for alerts. I was wondering if there was a way to test for console.log() output. For instance:

function_to_test = function(){
    var person = new Object();
    person.name = "Nicholas";
    person.age = 29;

    return(person.name);    //Nicholas
    return(person.age);     //29
});

I know I can have them return as one string, but for more complicated examples I'd like to be able to test the following:

function_to_test = function(){
    var person = new Object();
    person.name = "Nicholas";
    person.age = 29;

    console.log(person.name);    //Nicholas
    console.log(person.age);     //29
});

The Jasmine test looks something like:

it("should test for the function_to_test's console output", function(){
    expect(function_to_test()).toEqual("console_output_Im_testing_for");
});

Is there a simple way to do this that I'm just missing? I'm pretty new to coding so any guidance would be appreciated.

like image 748
Joe Susnick Avatar asked Nov 06 '13 23:11

Joe Susnick


People also ask

What is Jasmine used for in testing?

Jasmine Framework is an open-source JavaScript testing framework. It is a behavior-driven (BDD), development-inspired framework that is independent of any other frameworks. It is used for unit testing of both synchronous and asynchronous JavaScript test scenarios.

How do I test in node js with Jasmine?

In order to test a Node. js application, the jasmine framework needs to be installed first. This is done by using the Node package manager. The test code needs to be written in a separate file, and the word 'spec' should be appended to the file name.

Can JS read from console?

However, what is not understandable is using JavaScript which runs only the browser, to get input from a systems command line. Doing this is possible using NodeJS. It will require us to use the readline() package. With this package, we can then accept user input and even output it to the console.


1 Answers

There are a couple of things that are wrong with your code above. As pointed out, having multiple return statement will not work. Also, when defining a new object, it is preferable to use the object literal syntax over using new Object.

What you would normally do is something more like this:

var function_to_test = function () {
  var person = {
    name : "Nicholas",
    age : 29
  };

  return person;
};

Using the function above, there are a couple of ways you could test this. One it to mock out the console.log object using Jasmine Spies.

it("should test for the function_to_test's console output", function () {
    console.log = jasmine.createSpy("log");
    var person = function_to_test();
    expect(console.log).toHaveBeenCalledWith(person);
});

The other is to test the actual output of the function.

it("should return a person", function () {
    var person = function_to_test();
    expect(person).toEqual({ name : "Nicholas", age : 29 });
});
like image 197
Jason Avatar answered Sep 20 '22 14:09

Jason