creating a node module ❤

Part III: How to create a node module

bu kinoshita
the-zero
Published in
5 min readAug 22, 2017

--

Let’s learn how to create a small and simple node module with some ES2015 features and more. This is part 3 where I will cover tests, continuous integration and publishing a node module. If you missed part 1 and part 2, check here:

Before publishing, it's always good to make sure it works as expected

Now that we have everything done and working, we can create a couple of tests to make sure it's working as expected. Some people choose to create tests at the beginning, before writing any code. It doesn't work for me that way, but it's just personal preferences.

For the sake of simplicity, I'm covering only the main function which is the one on the root of our project, ./index.js.

Before writing any tests would be good to decide what are going to test. We know that we are going to test only the main function. And the expected output is a message saying that the pizzas that I ordered are ready.

$ yarn test
  • Make sure it returns a message saying the pizza I ordered is ready with the toppings and size I've chosen.
  • Make sure it works with different sizes and more than one topping.
  • Make sure it returns an error if the size is not available.
  • Make sure it returns an error if I don't choose any toppings.

Make sure it returns a message saying the pizza I ordered is ready with the toppings and size I've chose

On the first post, we installed ava to run our tests. If you're not familiar with ava take a look at their repository and documentation just to get familiar with it.

test when making one small BBQ Chicken pizza

To create a test you call the test function you imported from AVA. The first argument must be t.

On the first test, I created a const called pizza where it waits for the function to be resolved.

t.is(value, expected, [message]) asserts that value is the same as expected. So we assert that the value resolved on pizza is the same as the expected string.

Make sure it works with different sizes and more than one topping

This test should be the same way we created the first test. But instead of using only topping: ['BBQ Chicken'], you should include another topping as well and change the pizza size.

Make sure it returns an error if the size is not available

This time we need to make sure that our function throws an error correctly when we the customer chooses a size that's not available.

test when has no sizes

t.throws(function|promise, [error, [message]]) asserts that function throws an error, or promise rejects with an error.

The same way we had to wait for the promise to be resolved, here we have to wait for the function to throw an error because the size `a` it's not available.

We use t.is() again to assert that the error.message is the same as the expected string.

You can create as many tests as you wish. This is only an introduction on how tests work with AVA.

Continuous Integration

Well, continuous integration is great! If you're not using, you should try it. It will help automate your tests and deployments.

Although the one I like most is CircleCI, we will be setting up CI with Travis-CI.

Make sure you create an account on Travis with your Github account and sync your repositories.

travis-ci

Now that you have your repositories set up on travis, you need to create a .travis.yml file on the root of your project.

.travis.yml

language to be tested and the node versions to be tested. Since async/await was introduced on node v7.3 we don't want to run the tests on node < 7. Otherwise, the tests will fail.

Push your changes to Github and travis will be automatically triggered and the tests will start running.

If you want, you can add a script to deploy your app after the tests pass. Check here.

Publishing a node module

With all my node modules, I always publish my package with np. np is a better npm publish. And here is why:

  • Interactive UI
  • Ensures you are publishing from the master branch
  • Ensures the working directory is clean and that there are no unpulled changes
  • Reinstalls dependencies to ensure your project works with the latest dependency tree
  • Runs the tests
  • Bumps the version in package.json and npm-shrinkwrap.json (if present) and creates a git tag
  • Prevents accidental publishing of pre-release versions under the latest dist-tag
  • Publishes the new version to npm, optionally under a dist-tag
  • Pushes commits and tags to GitHub
$ np

Check the documentation on their repository to install and use it. Their interactive ui is pretty straightforward and easy to use.

$ np

--

--