0

I have a Node.js Express REST API app that works. Good.

I have a Mocha/Chai/Supertest mock that tests the API app above. Good.

But I have to start the app and then independently run the mock test.

How can I run a single grunt command that starts the API app, let's it get up and going, and then runs the mock test?

Or do I need to run the API app in some kind of test mode (via env var) and have test-only logic somehow invoke the mock test?

I can try some things and get something to work, but what is the good way? (Avoiding overused phrase 'best practice'.)

jfathman
  • 788
  • 3
  • 8
  • 13
  • grunt concurrent might help with this. I use it to run nodemon and watch at the same time. – Kevin B Oct 03 '14 at 22:20
  • Thanks Kevin, I will look into grunt concurrent. I am also considering running the API and mock test in two separate Docker containers (based on the same Docker image) with execution of the mock test parameterized at Docker container startup. I could then run the two containers using Docker fig. Sounds a little complex, but it would be a useful pattern for future use. – jfathman Oct 04 '14 at 12:46

1 Answers1

0

You can do that with grunt-express-server and grunt-mocha-test, you will juste have to setup your task like below :

grunt.registerTask('test', ['express:test', 'mochaTest']);

This will run your express server with the config you have set for the test environement then run mocha when you run grunt test.

Since you are using supertest I suppose you are doing functionnal testing which means that you will be using the same database for developement and testing (if you are not mocking something). That can be time loosing and make your test fail because of bad data. Using two different environement makes sure of the state of your data when you are running the test.

You can still use grunt watch plugins to relaunch your test on file change if you don't want to have to do it manually.

Hope this helps

Fougere
  • 187
  • 3
  • 8
  • Thanks Fougere, this looks promising. I will try it early next week and follow up. I wonder if there is a race condition where the mochaTest can try to access the express app before it is fully initialized and listening for requests? – jfathman Oct 04 '14 at 22:13
  • You can have this behavior if you use concurrent if you do, review the configuration of the plugin. Grunt launch the task one by one so you are sure that when grunt run the test your server is already up and running. That's why some smart guys created the plugin concurrent, to to some independent task while grunt was doing some others. – Fougere Oct 05 '14 at 09:46
  • Using grunt-express-server and grunt-mocha-test works great. Does just what I need. It anticipates the startup race condition concern, and supports an 'options output' string that can be regex matched so the mock test does not begin until the API app emits the expected string denoting 'API ready'. It also terminates the API app after the mock test completes. Perfect. – jfathman Oct 06 '14 at 15:44