node.jsmocha.jslocomotivejs

Testing locomotive.js with Mocha


I am building an app with locomotive.js, and I am looking to build my test suite using the Mocha test framework. I am also new to TDD/BDD in general, so please take that into consideration. I am curious if anyone can point me in a good direction to start testing a locomotive based app.

My biggest question would be:

  1. How do I test a controller's actions?
  2. Can I test an initializer?
  3. Are there best practices around creating test request objects?

Solution

    1. Testing controller actions
      It depends on what exactly you want to test. Controller actions usually either return content, or pass (perhaps in case of errors) the request along the server stack, so testing them means using something like supertest to check for the correct responses (the supertest page also mentions how to use it together with Mocha)

    2. Can I test an initializer?
      Testing an initializer by itself is difficult, because they require to be run within the context of a LocomotiveJS application. However, you could create a test just booting up the application, which during the booting process will also run all initializers. I just added a simple Mocha-based testing framework to my Locomotive + Sequelize boilerplate project which shows how to boot the Locomotive app from within Mocha.

    3. Are there best practices around creating test request objects?
      If you mean how you can check responses to requests, look at the aforementioned supertest or perhaps mocha-headless.