AngularJS Testing: Protractor, Karma, Jasmine in a Yeoman App

16,623

Karma is a test-runner, so it runs your test. Jasmine is the framework that let you write test

In my opinion in Angularjs you :

  • must unit-test services, because your business code is there.
  • should unit-test controller, because users actions are there.
  • may unit-test custom directives (if you plan to share that directive with others, it's a must)

Protractor is made for E2E testing (tests navigation like a real user). It combines WebDriverJS with Jasmine and lets you write End-to-End tests (you simulate a real browser and taking real actions) with Jasmine syntax.

That kind of test is also really important in a web app.

You should not test everything, especially at the start of the project, those kinds of tests usually come with a high level of maintenance (i.e., when you change a screen you may have to change the test).

What I do is test the critical path and features. I made a reading app, so in my case, it was login, sign up, payment, access book, and access reader.

Share:
16,623
Andi Giga
Author by

Andi Giga

Updated on July 16, 2022

Comments

  • Andi Giga
    Andi Giga almost 2 years

    I use this yeoman generator: https://github.com/Swiip/generator-gulp-angular

    It installs three testing applications: Jasmine, Karma , Protractor According to this article (Should I be using Protractor or Karma for my end-to-end testing?), I should use: Karma for small tests of e.g. a single controller. Protactor if I want to test the whole app and simulate an user browsing through my app. According to this blog (http://andyshora.com/unit-testing-best-practices-angularjs.html) I would use Jasmine for unit testing and Karma for end-to-end integration tests.

    I guess Jasmine is the language where tests are written and the other two execute the code, is that correct? Also if I never wrote a test which is more important to learn first/ to focus on?

  • Andi Giga
    Andi Giga over 9 years
    Ok but what can I do with Protractor than? Is it also a test-runner and do I write there also in jasmine?
  • Andi Giga
    Andi Giga over 9 years
    Ah ok cool, how do test payment, because providers have transaction fees. Do have a cheap hidden product to save fees etc.? So I guess I start with learning Jasmine (like the blog article I posted) and the test suits are probably anyways controlled by gulp. Do have a good resource for learning Jasmine otherwise I just google for it?
  • Boris Charpentier
    Boris Charpentier over 9 years
    For payment I only check the access to the screen and the redirect, not the actuel payment. Jasmine has some doc jasmine.github.io/2.0/introduction.html but it's really more made to go and pick what you need, when you need it. What I use most is, spyOn, expect, toBe, toEqual, toHaveBeenCall, and specific to angularjs $httpBackend for network call stub.
  • Boris Charpentier
    Boris Charpentier over 9 years
    While I'm here, when you'r in test(ie having loading angularMock), you should know that you have to manually resolve promise calling $scope.$digest(); and $httpBackend.flush() for network call, the principle are that the angular team don't like asynchronous test, and so the behaviour in the test made that syncronous. And I think it's great :)