Testing the interaction with html and JavaScript using Jasmine is wrong?

4

JavaScript code has some html interactions like this example:

function retornaListaDeItens(argument) {
  return document.getElementsByClassName(argument);
}

I use the return of this function to perform operations in JavaScript, performing unit tests using Jasmine I came across the following scenario of doubts:

  • Is it wrong to test this function?

  • Is it possible to simulate my html to perform tests that interact with it?

  • The test framework uses Jasmine's own SpecRunner.html file and has no contact with the html where it has the list of items to return in this example, making it difficult for unit tests.

    The structure looks something like this:

    app
      lib
      --bootstrap
      --jasmine
      src
      --css
      --js
        --teste.js
      test
      --testeSpec.js
      index.html
    

    The test idea looks like this:

      it("retorno de lista de itens não deve ser vazio", function() {
        var item = "item";
        var list = retornaListaDeItens(item);
        console.log(list.length);
        expect(list).not.toBeLessThan(0);
      });
    
        
    asked by anonymous 27.01.2016 / 01:24

    1 answer

    1

    I saw that it solved the problem and I'm happy about it, but still the need to do real tests in the application exists in many cases.

    If you want to test an application as a user (simulate actions in HTML and wait for the returns to see if they are correct) you can use some lib to support what we call e2e (end to end) tests.

    These tests are more laborious and slower, but they try to emulate exactly the actions of a user within the system.

    I'd advise searching and reading about: SeleniumHQ , PhantomJS , WebdriverIO and Chimp .

    I confess that my knowledge with this type of testing is very shallow, but I am studying and deepening every day and I can already say that for large systems are very useful.

    Good studies!

    EDIT:

    I do not do my tests with jasmine, but with MochaJS , I advise you to take a look too.

        
    01.04.2016 / 14:45