Sign in

Simon Karanja
Aspiring comedian and a lover of anything with .js extension

Testing Node.js, Express with Supertest & Jest testing framework.

Passing tests

With a multitude of testing framework available for JavaScript and Node.js in specific, it can quickly become overwhelming when deciding which to choose. For the seasoned, the choices usually narrows down to Mocha paired with assertion library like chai. Although Mocha can also be used for testing on the browser side, I have found having a uniform test framework across the Node.js and browser (React) environment to be more rewarding, and there’s no other choice that can rival Jest. The beauty of Jest is the out-of-the-box support on React and zero configuration almost elsewhere.

Now, first things first. Let’s create…

State in functional components

Hooking on the subway

React hooks is a god’s sent feature that was introduced in React 16.8. In old days, if your component did not handle any local state, then you would normally write it as a stateless functional component. Simple and elegant. Now the problem came if you decided to introduce state to the said component. You would need to refactor your functional component to a class based component to enjoy the state & the lifecycles.

Now with the Hook API, you can easily use state, introduce side effects and so much more. …

Testing under the stars.

Unless you fancy the built-in fetch API, chances are that you are using or have used axios as your HTTP client. If so, when writing tests you may need to mock API requests. You could of course use mock functions provided by your test library in this case Jest, but I have found a nifty package called axios-mock-adapter to be excellent and natural when testing codes with axios implementation.
Lets write a test for a simple component that fetches some random todos. A fully working implementation can be be found here. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store