Git Product home page Git Product logo

testing-hapi's Introduction

testing-hapi

Build Status Code Climate

Example Hapi-backed API Server with testing, CI, and Swagger documentation generator.

Updated and tested with latest hapi packages as of 1/16/2018.

How to run

Requires Node v8.9.0+

  yarn install #install dependencies
  yarn start # start server
  yarn test # run tests

Details

  • Notable npms

    • hapi, joi
      • hapi is a popular web/services nodejs framework. It is strict about routing and validation out of the box and has a mature plugin/extension system. I've used express for a long time, but since trying hapi, I haven't really looked back. On the surface the two frameworks look similar. In my experience though, I found that hapi managed to scale much better with increasing complexity and made it easier to test, debug, and write better code.
      • joi is an awesome schema/object definition and validation library. This project uses it to enforce API input/output validation and generate documentation.
    • hapi-swagger
      • Swagger is an API framework and standard. hapi-swagger is a hapi plugin that generates awesome interactive API documentation & UI right from our code API definitions. I gotta say... it's really nice to keep everything in one place.
    • lab
      • lab is hapi's version of mocha. It's a test runner, nicely packaged with a linter and code-coverage reporter. Nothing you wouldn't expect here. (Unless you've never written tests)
  • Testing

    • Unit Tests (/test/unit/*)
      • Functions containing business logic are defined in isolation from the framework making them testable without a running server
    • Functional Tests (test/functional/*)
      • These tests are meant to target the API endpoints, covering functionality end-to-end.
      • We should try to write these tests in a way that they become easily exportable to run automatically via tools like New Relic Synthetics
    • ESLint
      • lab also includes a linter (eslint by default), which is executed when tests run. The default configuration can be customized via the .eslintrc.json file.
    • Code Coverage
      • lab analyzes the code and returns the code coverage ratio when running the test. It also points out which lines of code are missing coverage. A nice reminder to write tests for any newly added functionality.
  • Documentation

    • hapi-swagger is configured in app.js and generates a very nice html page with an interactive Swagger compatible API.
    • Once your server is running locally, visit http://localhost:3000 to check out the docs.
    • My typical workflow is to write the documentation first (by setting up the hapi routing #2BirdsWith1Stone), then to write the functional tests, then a combination of code and unit tests ala TDD until I'm satisfied with the results.
  • CI

    • This repo also integrates TravisCI, which runs the tests defined above on every pull request, blocking a merge if the test does not pass. Not very useful for a one person project, but crucial when a team of developers is involved.

TODO

  • add stubbing framework to imitate external service calls.
  • Figure out an easy way to test multiple hapi services together, in a microservice environment.

testing-hapi's People

Contributors

pashariger avatar megadix avatar mdupuy-octo avatar

Watchers

Nosherwan Adil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.