Is testing the interaction with html and Javascript using Jasmine wrong?

Asked

Viewed 242 times

4

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 ended up encountering the following scenario of doubts:

  1. It is wrong to test this function?

  2. It is possible to simulate my html to perform tests that make interactions with it?

The test structure uses the file itself Specrunner.html of Jasmine and has no contact with html where has the list of items to be returned in this example, it ends up hindering unit testing.

The structure looks something 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);
  });
  • Solved your problem?

  • Hello @durtto, well I studied good Javascript programming practices along with good testing practices and I found that much of what I was doing was not following these good practices, an example of this was to mix the search behavior on the screen along with some rules, well summing up separated my js making communication with the screen and my js actually making the business rule and performed the tests only in performing the business rules.

  • 1

    Good Emanoel. I’m glad you study like big people. It helps the community. Try to do the following, if you have found the answer, write it here and do not leave the question unanswered, as you help people who are in the same situation. The Community thanks you.

  • Hello @durtto, yes I intend to do that.

1 answer

1

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

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

I advise to research 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 are very useful.

Good studies!


EDIT:

I don’t do my tests with Asmine, but with Mochajs, advise you take a look too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.