Testing interaction with html and JavaScript using Jasmine is wrong?

JavaScript code has some interactions with html like this example:

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

I use the return of this function to perform operations in JavaScript, performing the unit test using Jasmine I ended up coming across the following scenario of doubts:

  1. Is it wrong to perform the test of this function?

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

A test structure uses the SpecRunner file itself.Jasmine's html and has no contact with the html where it has the list of items to be returned in this example, this ends up making unit testing difficult.

The structure is more or less like this:

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

The idea of testing is 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);
  });
Author: Junior Moreira, 2016-01-27

1 answers

I saw that solved the problem and I'm happy for that, 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 check if they are correct) you can use some lib to assist in what we call E2E (end to end) tests.

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

I advise you to search and read about: SeleniumHQ, PhantomJS, WebdriverIO and Chimp .

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

Good studies!


EDIT:

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

 1
Author: Willy Camargo, 2016-04-01 12:45:16