In software development, sometimes we aren’t sure how to solve a problem. It may happen when you are using a new API, framework, or just because you never had a similar problem before.
Using TDD in this scenario is hard. In order to write the test first, you need to be able to define the expectations, if you don’t know that, TDD is gonna be really challenging.
In Agile Development there’s a technique for this situation called Spike. This term comes from Extreme Programming, where “A spike solution is a very simple program to explore potential solutions”.
To implement a spike solution, you normally create a small program very quickly testing the problem in question. When you have the answers that you need, you go back and build it again, but this time using TDD.
I got very excited when I discover Spikes, because I have been doing it for a while,
and I really didn’t know that it had a name.
What about you, have you been writing spike solutions?
In the last week, I had a problem after including some new libraries in the project that I was working on. There was no exception occurring, but the Web Sockets stopped to work.
Unfortunately, we just realized the problem after some days. This gave me some hours of work (and some gray hairs), trying to figure out manually what commit breaks the web socket. Actually, it was my mistake, I didn’t write any integration test for the Web Socket.
After discovering the problem. (an integration problem with the lib spring-cloud-sleuth),
I decided to write a test for it. I don’t want to have the same problem again!
I had never written tests for Web Sockets before, and I didn’t find any clear and good example for it. Then, I decided to share my solution here, maybe it will help someone that needs it.
This is the websocket configuration using Spring:
And here I put the test using Stomp Client. Basically, the test connects to the Web Socket,
then subscribes to the topic and sends a message. I used the class BlockingQueue, in order to wait for the asynchronous response.
Lesson learned
Always write integration tests. It can prevent bugs when you upgrade libraries, or when you include new ones. It also can save some hours and headaches in the future.
If you want, you can clone the complete project from my github:
Webpack is a powerful tool for bundling modules, helping you to organize your code, bundling it
in a single js file.
Today, I’m going to create a simple project with webpack. My goal here is to show how to organize Js modules using this tool. Let’s get to it!
Installation
First, let’s install webpack globally. (You also need Node and Npm installed).
Project structure
This is the folder structure of our project:
app.js
The app file requires the module1 and module2.
module1.js
module2.js
index.html
In the index.html, we just include the bundle.js file. Webpack will compile the modules in this file.
webpack.config.js
This is the webpack configuration. Basically, we are saying to webpack where our entry file is and the bundle filename.
Compiling
Now we can run webpack:
It will compile our modules and create a bundle.js file.
Open the index.html file in your browser to see the result.
Development Server
In order to serve the static files compiled by webpack, we can use the webpack-dev-server.
It automatically updates the browser page when a bundle is recompiled.
Let’s install it:
Now we just run:
Now open: http://localhost:8080/ to see the result.
External Modules
Let’s say that we need a external module in our project, Jquery for example.
First, let’s install jquery using npm:
Then we just import and use!
Conclusion
You could see how is easy to organize a project in modules using webpack. This is just a simple example,
we can do a lot of other things with this tool, like uglyfy, minify, compress static assets, etc.
In the next post, we will use Webpack with Angular2, and build a web application!
I have been using the Lodash library a lot lately, then I decided to list here some functions that I consider very useful. You will notice how it can simplify your code, making it more elegant, cleaner, and functional.
Lodash, is a tool kit of JS functions that provides methods for manipulating objects and collections. It’s like a utility belt!
Filtering
In order to filter collections, we can use the filter method. It’s returns only those objects that contain the specified key, and it’s also supports nested objects.
Grouping objects
The groupBy method takes an array of objects and groups them by some field or condition.
Merging Objects
The merge method takes multiple source objects, and merges their properties together into an object.
Selecting properties to create a new object
The pick method creates an object composed of the picked object properties.
Chaining
Lodash allows us to chain the collection functions instead of using a bunch of temporary variables, making your code more readable.
Let’s say that we have an array of pets and we need:
get all pets that are dogs
get just the dog name
order by name
join the names with commas
More about Lodash
There are hundreds of useful functions available in the Lodash library. Here I just listed some simple examples, but they’re enough to realize how useful it can be for your Js projects.
If you want to learn more, Lodash website is an excellent resource. Feel free to write your questions in the comment box.
Testing dates in unit tests is not always an easy stuff, In this post, I’m going to show an easy and elegant way to do that in JS using Jasmine.
First, take a look at the function below. It’s calculating the difference between today and a date passed by param. To make the example easier, I’m using momentJS library to work with dates.
How can we test it? The moment object will always return the current date, how can we write a test that can pass every day?
In order to solve this problem, we can use Jasmine Clock to mock the current date.
If you are using Jasmine as your test tool, it’s an easy way to mock dates!
However, if you are not using Jasmine, there is another way to solve this problem, you can create a date utility class and mock it. I usually do it using Java for example, but it’s a topic for another day.