2

I have a javascript function like this

function formatInput(input) {
  //want to test only this immediate statement
  var type = input.ipType.toString().toLowerCase().trim();
  var afterVormat = someFunction(type);
    return afterFormat;
  }

I am able to test this function(value of afterFormat) correctly , but is it possible/how to test a specific line in function since I am not returning type.

For example I want to test if var type is as it is expected

brk
  • 48,835
  • 10
  • 56
  • 78

3 Answers3

2

Is it possible/how to test a specific line in function?

The immediate answer: no.

The solution

One of the outcomes of adhering to TDD is that it forces you to build code in isolated, testable blocks. This is a direct consequence of the fact that you cannot perform test(s) of the individual lines of a function. In your case the solution is to restructure your code to:

var type = function(){
    return input.ipType.toString().toLowercase().trim();
};

function formatInput(input) {

  var type2 = type();
  var afterVormat = someFunction(type);
  return afterFormat;
  }

Now you have made type an isolated block that you can test.

If you combine this with use of Sinon.JS you can use a spy to test that an invocation of function formatInput() will also result in the invocation of type() and thereby you know for sure that var type2 has been assigned the intended value.

rabbitco
  • 2,790
  • 3
  • 16
  • 38
0

I’m not aware of any specific and more advanced unit testing method/system for javascript, but you can have a simple assertion function to test individual lines of code for debugging purpose like this:

function assert(condition, message) {
    if (!condition) {
        message = message || "Assertion failed";
        if (typeof Error !== "undefined") {
            throw new Error(message);
        }
        throw message; // Fallback
    }
}

(Code taken from TJ Crowder's answer to another question.)

Then you can just use it to check for instance the var type like this:

assert(type == "something expected here and shall throw an error otherwise");
Community
  • 1
  • 1
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • The OP's problem here is not "how do I assert" but "how do I test *this specific line*". Your answer here is answering the former, which the OP did not ask, but not the latter, which the OP did ask. Also, when you use parts of other answers, please link to the answers *directly*. Linking to the question that they answer is not precise enough to satisfy this site's license. – Louis Nov 16 '16 at 11:47
  • @Louis I have used assertions to test such conditions, but I get your point, assertions are not for testing, but for breaking the code if conditions not met. I don't have much experience here on SO and I'm still learning how to compose proper Q&A. Thanks for your guidelines. – Christos Lytras Nov 16 '16 at 13:37
-1

You can use console.log() function for that. As below.

function formatInput(input) {
    var type = input.ipType.toString().toLowerCase().trim();
    console.log(type);
    var afterVormat = someFunction(type);
    return afterFormat;
}

Also you can use debugger; also, to debug the code line by line.

function formatInput(input) {
    var type = input.ipType.toString().toLowerCase().trim();
    debugger;
    var afterVormat = someFunction(type);
    return afterFormat;
}

and just press F10 key to debug the code and you can check the values in console.

Sergej
  • 1,082
  • 11
  • 27
Udayraj Khuman
  • 556
  • 2
  • 6
  • 11
  • I guess that wont suffice the purpose of javascript unit testing. For example i am looking for something like `expect(type).to.be.eqaul('someValue')` – brk Nov 16 '16 at 06:29
  • you can put the condition as below under the 'type' declaration line if(type == undefined){ return false; } – Udayraj Khuman Nov 16 '16 at 06:32