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

Front-End Testing with Mocha

Front-End Testing with Mocha

A presentation I gave at the July 26, 2014 meeting of the Charlotte, NC, Front-End Developers Meetup

vjwilson

June 26, 2014
Tweet

More Decks by vjwilson

Other Decks in Programming

Transcript

  1. Types of Tests TESTS. LOTS OF TESTS. • SMOKE TESTS

    • INTEGRATION TESTS • USER ACCEPTANCE TESTS • REGRESSION TESTS • UNIT TESTS
  2. Types of Tests TESTS. LOTS OF TESTS. • SMOKE TESTS

    • INTEGRATION TESTS • USER ACCEPTANCE TESTS • REGRESSION TESTS • UNIT TESTS
  3. Unit Tests TESTS. LOTS OF TESTS. ENSURE YOU BUILD THE

    THING RIGHT • code functions as designed
  4. Unit Tests TESTS. LOTS OF TESTS. ENSURE YOU BUILD THE

    THING RIGHT • code functions as designed • run when code it compiled
  5. Unit Tests TESTS. LOTS OF TESTS. ENSURE YOU BUILD THE

    THING RIGHT • code functions as designed • run when code it compiled • run as a suite of tests
  6. Unit Tests TESTS. LOTS OF TESTS. ENSURE YOU BUILD THE

    THING RIGHT • code functions as designed • run when code it compiled • run as a suite of tests • better documentation
  7. Unit Tests TESTS. LOTS OF TESTS. ENSURE YOU BUILD THE

    THING RIGHT • code functions as designed • run when code it compiled • run as a suite of tests • better documentation • uncover bugs pre-QA
  8. TDD TDD vs BDD 1. Write tests 2. Tests fail

    3. Write code 4. Tests pass
  9. TDD TDD vs BDD 1. Write tests 2. Tests fail

    3. Write code 4. Tests pass 5. Refactor
  10. TDD TDD vs BDD 1. Write tests 2. Tests fail

    3. Write code 4. Tests pass 5. Refactor 6. Repeat
  11. BDD TDD vs BDD 1. Establish goals/vision 2. Involves team

    to describe the behavior of an app.
  12. BDD TDD vs BDD 1. Establish goals/vision 2. Involves team

    to describe the behavior of an app 3. Create code based on the behavior descriptions
  13. BDD TDD vs BDD 1. Establish goals/vision 2. Involves team

    to describe the behavior of an app 3. Create code based on the behavior descriptions 4. Automate the testing of your code (feedback/ regression testing)
  14. 1. Describe Behaviors (STAKEHOLDER + TEAM) 2. Write Scenarios/Specs (DEVELOPERS)

    3. Run Tests (TESTS FAIL) 4. Write Code for Tests to Pass 5. Run Tests (TESTS PASS) FAIL PASS REFACTOR
  15. The hardest single part of building a software system is

    deciding precisely what to build. - Fred Brooks
  16. • Install mocha as a node module and let it

    build a skeleton npm install -g mocha; mocha init . or • Just download files from the repositories https://raw.githubusercontent.com/visionmedia/mocha/ master/mocha.css https://raw.githubusercontent.com/visionmedia/mocha/ master/mocha.js
  17. Then, you need an assertion library. • Grab the browser

    version right from the website: http://chaijs.com/chai.js
  18. Write a test suite. • Mocha will automatic run tests

    in these places: tests.js /test/*.js
  19. Chai Matchers MOCHA and CHAI ANATOMY var expect = chai.expect;

    describe ( "Hello", function(){ it( "Says hello to everybody", function(){ expect( hello() ).to.be.equal( "Hello World!" ); }); });
  20. More Matchers MOCHA and CHAI ANATOMY • ok • to

    / be / is / a ... • true • include • true • false • exist • undefined • null • empty • equals • above • below • (and many more...) For all the BDD matchers, see: http://chaijs.com/api/bdd/ For the TDD-style matchers, see: http://chaijs.com/api/assert/
  21. Multiple Test Cases MOCHA and CHAI ANATOMY var expect =

    chai.expect; describe ( "Hello", function(){ it( "Says hello to everybody", function(){ expect( hello() ).to.be.equal( "Hello World!" ); }); it( "Says hello to a specific person", function(){ expect( hello( "Dolly" ) ).to.be.equal( "Hello Dolly!" ); }); });
  22. JavaScript MOCHA and CHAI ANATOMY function hello( greetee ) {

    if ( greetee === undefined ) { return "Hello World!"; } else { return "Hello " + greetee + "!"; } }
  23. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  24. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  25. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  26. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  27. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  28. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  29. Test Runner MOCHA and CHAI ANATOMY <!DOCTYPE html> <html> <head>

    <title>Mocha - Expect</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script src="chai.js"></script> <script src="main.js"></script> <script>mocha.setup('bdd')</script> <script src="tests.js"></script> <script> mocha.run(); </script> </body> </html>
  30. beforeEach() MOCHA and CHAI ANATOMY var runnerWideFoo = []; beforeEach(function

    () { runnerWideFoo.push('runner'); }); describe('some suite', function () { beforeEach(function () { runnerWideFoo.push('suite'); }); it('should equal bar', function () { expect(runnerWideFoo).to.be.equal(['runner', 'suite']); }); });
  31. afterEach() MOCHA and CHAI ANATOMY describe('some suite', function () {

    var suiteWideFoo = 1; afterEach(function () { suiteWideFoo = 0; }); it('should equal 1', function () { expect(suiteWideFoo).to.be.equal(1); }); it('should equal 0 after', function () { expect(suiteWideFoo).to.be.equal(0); }); });
  32. Enter... Sinon BEYOND MOCHA and CHAI Through these tricks and

    the skill of perjured Sinon, the thing was credited, and we were trapped, by his wiliness, and false tears, we, who were not conquered by Diomede, or Larissan Achilles, nor by the ten years of war, nor those thousand ships. -Vergil, Aeneid II:195-199
  33. Stubs - Matchers BEYOND MOCHA and CHAI • toHaveBeenCalled() •

    toHaveBeenCalledWith() • andCallThrough() • andReturn() • andThrow() • andCallFake()
  34. You should not be afraid to delete tests that are

    no longer providing value, no matter whether you originally planned to keep them or not. We tend to treat tests as these holy creatures that live blameless, irreproachable lives once they have sprung into existence. Not so. The maintenance required to keep a test running weighs against its value in further development. Sometimes these lines cross, and the test simply becomes a burden on the project. Having the skill and experience to recognize a burdensome test is something we should be bringing to our clients, as well as the fortitude to rewrite it, rethink it, or delete it. - Adam Milligan, Pivotal Labs