In a previous post I show how you can you create a node.js web server without any middleware or additional dependencies. One of the downsides of that approach is that it makes the http server setup more difficult to maintain and read. In today's post I will show how to refactor the code to be more readable and how to setup your unit tests. In keeping with the spirit of the original requirements I didn't add any production dependencies, but I did add nodemon and Jasmine as dev dependencies. You don't need nodemon, but it does make development much easier. So you could create a full production node.js web server with full unit test coverage and only add a single dependency (and its child dependencies) other than node/npm itself. Let's get started.
The first thing I did was break out the http server setup into its own class. If you compare the server as it is now to the original post you'll notice a couple of things right away. First I created the server as a class and required a router to be supplied in the constructor. The second thing is that I pull the path logic out of the server. As a result of removing a lot of the logic the if/else if/else block in the web server is a lot cleaner and easier to read. The main reason I setup the server to accept a router in the constructor was so that I can mock the router in my unit tests. The rest of the code is very simple and straightforward. I've been toying around with native iOS development using swift on and off for about a year now. One topic that I haven't looked into as much is how to do automated testing. I've looked at XCTest but I just don't like that I have to start all of test names with the word test. I believe that the test name should descriptive of the work being tested and appending each test with the word test does not make sense to me. So after some searching I came across Quick and Nimble test frameworks. I'm coming from hybird mobile development and using Jasmine to test my JS files. So the Quick and Nimble structure is just like Jasmine with nested describes and it statements to structure your tests. So that combined with not having to append each test name with test was enough for me to go a little deeper with Quick and Nimble. I thought I would write this brief post about my experience setting up a sample application with Quick and Nimble to help anybody else out there looking for something besides XCTest. Be sure to checkout the finished project at my GitHub.
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|