What is it?

HTTP Endpoint tests are a type of integration test. In other words, we're testing a large chunk of the code without mocks, in order to verify the operation of a set of dependent components together.

In particular, we're using tools to drive the HTTP API exposed by the backend server and verify it's behaviour.

These tools are great for testing Node.js applications; they also work equally well for testing Rails, PHP, Java, .NET or Python applications. All that is required is a REST API.

Why do it?

There are several reasons you may want to do this.

Contract Validation

We live in a distributed world. Modern applications are loosely-coupled distributed systems where the components, from your mobile app, to your backend, to your hosted databases, all communicate over the web. And most likely, they do this over HTTP.

Each of these HTTP APIs defines a contract between systems. These contracts turn out to be excellent boundaries for automated validation.

Acceptance Testing

"I'm just a front-end guy" you may say. "I don't need to test my third-party backend. If those guys break it, it's their fault!"

Except when it becomes your problem...

Cook up a suite of endpoint tests and you can immediately and automatically validate new versions of services you depend on.

Documentation

A clean set of readable tests make excellent documentation for your API. Documentation you can run. Documentation that never gets out of date. Fluent, expressive tools like supertest, mocha, and chai will make you wonder why you ever tried to document with comment parsers.

And of Course, Verification

If you're writing your own backend, your endpoint tests will be another safety net allowing you to iterate fearlessly on your code.

Just Show me How Already!

Because supertest calls are asynchronous, we would normally have to use mocha's done() function to make sure the test waits until the request has completed. However, I prefer to use the super-test-as-promised package to express this more cleanly.

You can even test authenticated endpoints by sending cookies or authentication headers.

Conclusion

Supertest and Mocha's fluent APIs make writing HTTP endpoint tests easy. Such a test suite becomes a very readable description of the contract between client and server. HTTP endpoint testing is another powerful weapon in your quality arsenal.