Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Présentation tests javascript

Présentation tests javascript

Tests d'une application Backbone avec Karma, Jasmine et Sinon avec intégration continue sous Grunt.

Atala

May 02, 2014
Tweet

Other Decks in Programming

Transcript

  1. Stack • Karma : test runner (+ karma-cli) • RequireJS

    • Jasmine : test framework • Sinon : test utilities • Grunt-karma : integration avec grunt
  2. Karma • Test runner développé par google • Vidéo de

    présentation : http://www.youtube.com/watch?v=MVw8N3hTfCI • Nombreux plugin dispo (browsers, frameworks, ..) : npm install karma-* • Conf : karma.conf.js
  3. Anecdote Karma était appelé « Testacular » avant, mais ils

    ont décidé de changer de nom. Je ne sais pas pourquoi.
  4. Karma.conf.js Clés importantes : • Files : fichiers qui seront

    servis par karma. Le flag included=false indique qu’il faudra les ajouter ‘manuellement’ (via requirejs) • Browsers : browsers utilisés pour les tests • SingleRun : tests en continu ou non
  5. test-main.js • Equivalent au main.js de l’app ! • Indique

    les fichiers à servir, qui seront accessibles dans chaque test
  6. Jasmine : sample test describe('just checking', function() { it('works for

    underscore', function() { // just checking that _ works expect(_.size([1,2,3])).toEqual(3); }); });
  7. Jasmine : test avec import define(['underscore', 'jquery', 'views/personal-infos'], function(_, $,

    PersonalInfosView) { // test import describe('just checking', function() { ! it('works for underscore', function() { // just checking that _ works expect(_.size([1,2,3])).toEqual(3); }); }); });
  8. Sinon • Fake server (bug avec karma à régler cet

    aprem) ! • Spies (= function call monitoring) ! • Mocks
  9. Sinon : test avec serveur describe('testing a fake server', function()

    { beforeEach(function() { console.log(sinon); this.server = sinon.fakeServer.create(); }); afterEach(function () { this.server.restore(); }); it('fakes a jquery ajax request', function() { // lets try to initialize this.server.respondWith("GET", "fake.ledej.fr/pro/api/session", [200, { "Content-Type": "application/json" }, "{ 'token': 'allez', 'awesome': 'viens', 'username': 'on est', 'channel_id': 'bien' }"]); jQuery.ajax({ url: "fake.ledej.fr/pro/api/session", // success: callback });