TDD in JavaSCRIPT

Name: Yoan Ribeiro

Title: Full-Time Learner

Works @ JS-Republic

@yoanribeiro

Yoanribeiro

SUMMARY

  • Unit Testing
  • The concept
  • Demo
  • Pros/cons

TDD = Unit TESTING

You make unit test by using TDD but make unit test is not necessary using TDD

Unit TESTING

when you test a small part of code isolated from the rest.

(e.g. function or class) 

UNIT TESTING - RULES

  • AUTOMATIC
  • VERY FAST (<300ms)
  • ISOLATED (no side effect)
  • VERIFY A SMALL UNIT

Unit testing - JS  FLAVOR Example

Take, the example of a function responsible of returning the number of specific dom elements.

 

We will use Mocha as test runner

 

 

Chai as assertion library

Unit testing - JS FLavor EXAMPLE

REMEMBER : Given   WHEN   THEN

Prepare the case you want to test

REMEMBER : Given   WHEN   THEN

Call what you want to test

REMEMBER : Given   WHEN   THEN

Verify the behavior, result of what you test

REMEMBER : Given   WHEN   THEN

The three parts of every unit test

Test DRIVEN DEVELOPMENT

First test your use case, then implement your use case, refactor and start over until functionality is over.

TDD, AN OLD STORY

1957 - Digital Computer Programming, D.D. MCCRACKEN

1968 - Report of The Nato Software Engineering Conference, P. Naur

1972 - The Humble Programmer, Edsger W. Dijkstra

2000 - Test-Driven Development by Example ,  Kent Beck

TDD is A methodology

TDD = Write all your testS first

TDD is A CYCLE

Red
Green
REFACTOR

Red / Green / Refactor

Write a test

which fails

Refactor by keeping it green

Make the test

pass

Write a unit test which test a case not yet implemented. Obviously, this test will failed !

It's what we want

Red / Green / Refactor

Write a test

which fails

Refactor by keeping it green

Make the test

pass

Write an implementation which resolve the case that you just test (and the previous test cases) and no more

Red / Green / Refactor

Write a test

which fails

Refactor by keeping it green

Make the test

pass

Refactor to improve the general implementation by keeping every use case green.

Red / Green / Refactor

Write a test

which fails

Refactor by keeping it green

Make the test

pass

DEMO

Pros

  • Better code coverage
  • Better architecture (less coupling)
  • Gives us a degree of certainty 

CONS

  • Require good understanding of the used technology
  • Can be a little bit time consuming at the beginning

CONCLUSION

TDD is nice to increase quality


TDD should not be a dogma


To make unit tests every single time is already good thing

https://github.com/Yoanribeiro/tdd-js-star-april

Title Text

Made with Slides.com