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

An exciting intro to JavaScript unit testing

Juan
September 20, 2019

An exciting intro to JavaScript unit testing

Utah JS 2019

Juan

September 20, 2019
Tweet

More Decks by Juan

Other Decks in Programming

Transcript

  1. Unit testing is a software testing method by which individual

    units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use. @juanlizarazog
  2. •Testing individual units of code • Unit is the smallest

    testable part What is unit testing? @juanlizarazog
  3. •Trust changes you make on existing (already tested) code. •Code

    quality •Changes occur quickly •Documents your own code •Automation Why? @juanlizarazog
  4. 1. Learn some key concepts @juanlizarazog @juanlizarazog Matchers •It’s just

    a function •Implements a boolean comparison expect(1 + 1).toBe(2);
  5. 1. Learn some key concepts @juanlizarazog @juanlizarazog Specs •Short of

    specification •Group of expectations (that test the state of the code). •spec -> test test('adds 1 + 1 to equal 2', () => {
 expect(sum(1, 1)).toBe(2);
 });
  6. 1. Learn some key concepts @juanlizarazog @juanlizarazog Specs •Short of

    specification •Group of expectations (that test the state of the code). •spec -> test it('adds 1 + 1 to equal 2', () => {
 expect(sum(1, 1)).toBe(2);
 });
  7. 1. Learn some key concepts @juanlizarazog @juanlizarazog Suites •Group of

    tests •Help us organize our units describe('sum()', () => {
 // specs
 });
  8. @juanlizarazog test('adds 1 + 2 to equal 3', () =>

    {
 expect(sum(1, 2)).toBe(3);
 }); function sum(a, b) {
 // return b + a;
 // return a + b;
 // return Number.parseInt(a, 10) + Number.parseInt(b, 10);
 // or anything else, we just care that the result is the sum
 }
  9. Recap 1. Pick a library or framework 2. Learn some

    core concepts 3. Do not test implementation details 4. Write tests! @juanlizarazog