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

Behavior Driven Development - Node.js Paris meetup

Behavior Driven Development - Node.js Paris meetup

Presentation made during Node.js Paris meetup (with Julien Pavon)

Sylvain PONTOREAU

June 13, 2018
Tweet

More Decks by Sylvain PONTOREAU

Other Decks in Programming

Transcript

  1. Who we are? Software Craftsman at Coach at Premier Field

    Engineer at Trainer Co-organizer TDD and BDD paranoiac! TypeScript fanboy!
  2. What is BDD? This is an enhancement of TDD introduced

    by Dan North in 2003 when he was a TDD trainer. The aim of BDD is to re-explain the basics of TDD replacing the word Test with Behavior. Dan North defines it as: "TDD done well".
  3. Common mistakes… Too much people think that TDD is about

    unit testing while all is about acceptance testing Too much people think that BDD is about UI testing while all is about acceptance testing
  4. BDD vs UI tests We are not talking about the

    same thing here. BDD is a way to drive your development by focusing on the system behavior. UI tests is about executing tests on the UI layer.
  5. BDD == TDD Yes and No… For programmers BDD is

    just TDD. BDD brings something more than TDD: communication. Help all the project team to work together.
  6. Think about that… As a user In order to validate

    my cart I need to be authenticated if(this.user.isAuthenticated) { this.cart.validate(); }
  7. The Gherkin language Gherkin helps you to describe software’s behaviors.

    This "documentation" help to test automatically your code. Let’s see an example..
  8. Gherkin: The bad way Scenario: Search with a browser Given

    I open a web browser And I navigate to https://google.fr When I fill the search input "TypeScript" And I click on the search button Then I am redirected on the results page And I should see links related to "TypeScript"
  9. Why is it so bad? It describes how to search

    on Google with a browser. If you want to do the same with a mobile assistant, you have to write another scenario. But behavior is the same…
  10. Gherkin: The bad way (2) Scenario: Search with Google assistant

    Given I’m on my smartphone And I say "Ok Google" When I say "Search for TypeScript on the web" Then I am redirected on the results page And I should see links related to "TypeScript"
  11. Gherkin: The good way Scenario: Search for TypeScript on Google

    When I search for "TypeScript" on Google Then I should get results related to "TypeScript"
  12. Why is it better? It describes how to search on

    Google and that’s all. It can be used to execute tests on each platform you want. It's just a single scenario -> easier to maintain.
  13. Let's take a look at Cucumber A Behavior Driven Development

    framework. First built in Ruby and now available for many languages. Gherkin parser and execution. Version 4
  14. Features Gherkin features and scenarios. Scenario: Search without a value

    When I search without any value Then I have an error
  15. Steps Given, When, Then as functions. import { expect }

    from "chai"; import { Then } from "cucumber"; Then("I have an error", function() { const expected = 400; expect(this.actual).to.be.equal(expected); });
  16. World Scenario execution context. import { World, setWorldConstructor } from

    "cucumber"; declare module "cucumber" { interface World { actual: number | undefined; } } setWorldConstructor({ actual: undefined });
  17. Hooks Run code during lifecycle import { Before, CallbackStepDefinition, HookScenarioResult

    } from "cucumber"; import { Database } from "sqlite3"; Before(function(scenario: HookScenarioResult, done: CallbackStepDefinition) { this.database.run( "CREATE TABLE Website (url TEXT, title TEXT, description TEXT)", err => { done(); } ); });
  18. A real world example? "Guys, you can't implement a complete

    stack with BDD during a meetup, you need to built a project!"
  19. An example with IoC? Let's go! ☺ Controller Container SQLite

    Provider Dependency Dependency Dependency Dependency Dependency Dependency
  20. An example with IoC? Let's go! ☺ Controller Container SQLite

    Provider SQLite In Memory Dependency Dependency Dependency Dependency Dependency Dependency
  21. Repo & Posts Samples: https://github.com/spontoreau/express-typescript-cucumber TypeScript & Cucumber – Getting

    started: https://sylvain.pontoreau.com/2018/04/30/typescript-cucumber-getting-started/