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

Unit Testing in JavaScript Using Mocha & Chai

Unit Testing in JavaScript Using Mocha & Chai

Presented at the Hill Country JavaScript Meetup - July 20, 2016

Eric Carraway

July 20, 2016
Tweet

More Decks by Eric Carraway

Other Decks in Programming

Transcript

  1. UNIT TESTING WITH MOCHA + CHAI QUESTIONS WE’LL ANSWER TONIGHT:

    ▸ Who does unit testing? ▸ What is unit testing? ▸ When should I use unit testing? ▸ Why do developers use unit testing? ▸ How do I get started with unit testing? 2
  2. 3

  3. 4

  4. ERIC CARRAWAY MY PROGRAMMING BACKGROUND ▸ High School Computer Science

    in 1995 ▸ Borland Turbo Pascal ▸ 12 Year Gap ▸ Music School, Teaching Career ▸ Music Visualizations (2007) ▸ Self Study + Coding Bootcamp (2014) 5 Contact: [email protected]
  5. 6

  6. 7

  7. LEARNING TO CODE THREE-LEGGED STOOL ▸ Tutorials, step-by-step books ▸

    codecademy.com ▸ Algorithm Challenges ▸ codingbat.com ▸ coderbyte.com ▸ Projects ▸ Ideation, Creativity 9
  8. 10

  9. WE WANT MAKE A PACKAGE OF GOAL KILOS OF CHOCOLATE.

    WE HAVE SMALL BARS (1 KILO EACH) AND BIG BARS (5 KILOS EACH). RETURN THE NUMBER OF SMALL BARS TO USE, ASSUMING WE ALWAYS USE BIG BARS BEFORE SMALL BARS. RETURN -1 IF IT CAN'T BE DONE. Nick Parlante, CodingBat.com A FAVORITE ALGORITHM CHALLENGE 12
  10. ALGORITHM CHALLENGES ARE GENERALLY PURE-FUNCTIONS PURE-FUNCTIONS ARE A GREAT INTRODUCTION

    TO UNIT TESTING ▸ Tests that are only concerned with input and output of a function ▸ Focus narrowly before worrying about ▸ Dependency injection ▸ Global state ▸ Asynchronous behavior ▸ Mocks ▸ Browser automation 16
  11. LEARN TO CODE IN THE BROWSER CODINGBAT.COM - INSTANT FEEDBACK

    ▸ We want make a package of goal kilos of chocolate. ▸ We have small bars (1 kilo each) ▸ and big bars (5 kilos each) ▸ Return the number of small bars to use, assuming we always use big bars before small bars ▸ Return -1 if it can't be done. 17
  12. 18

  13. 20

  14. UNIT TESTING IN JAVASCRIPT QUICK REVIEW - BEFORE MOCHA +

    CHAI ▸ Who / What / When / Why / How ▸ Three-Legged Stool Analogy ▸ Algorithm Challenges (Test-Driven Approach) ▸ _____________________________________________________ ▸ Mocha + Chai ▸ Rock, Paper, Scissors ▸ Rock, Paper, Scissors, Lizard, Spock ▸ https://github.com/ericcarraway/codingbat-js/tree/master/ rockPaperScissorsLizardSpock 21
  15. 24

  16. GETTING SET UP OPTIONS FOR FILE NAMES, DIRECTORIES ▸ Mocha,

    by default, looks for a directory called “test” ▸ this allows you to simply run `mocha` ▸ Another approach: `.spec` right next to your module 26
  17. 27