Swizec Teller - a geek with a hatswizec.com

    Blackbox testing node.js apps

    One of the best features in Djangois the test client that comes with the test framework -> blackbox testing baby, yeah!

    Black box flight recorder

    Blackbox testing is my favourite kind of testing. Much more useful than unit tests and generally a lot easier to write, maintain and deal with. And that's because rather than making sure all your functions and deep internals are behaving like they should, you only test what really matters in a web app.

    Does the right output happen for this specific input?

    Your users don't care that you have full unit test coverage. They will never see the output of all your internal functions and methods and database drivers and whatnot. All they care is that if they put in X they get the correct Y out.

    The two main benefits of blackbox testing for a developer are:

    1. Low maintenance tests - you don't have to rewrite the whole test suite when refactoring the deep internals of your app
    2. Integration tests - since you're only matching input to output you are testing your whole chain, everything from routing, to database connections and the business logic of the app itself

    Although it's still prudent to write enough of these tests to check for correctly returning errors, making sure your app doesn't crash on strange inputs and so on.

    It's also important not to forget unit tests for core business logic. That's algorithmic stuff, algorithms should have unit tests attached to them. But in general, web apps aren't algorithms, they're systems. And systems need blackbox tests.

    Doing it in node.js

    Ok so blackbox testing is an awesome fit for web apps. But how do you do them?

    In django this is simple, when in a test you just say something like

    response = self.client.get("/my/url")
    self.assertEqual(response.status, 200)

    and check the response is what you expected. The test client automagically ensures there is a fake server running, that it takes requests, responds and is then torn down when you're done testing.

    Until recently the only way I knew of doing this with node.js has been to run the server and send it requests. But that's a bit lame, after all, I want to just run the tests. Of course I'm going to forget to run the server every single time.

    Not to mention you have to keep restarting it when you change the code.

    Enter Supertest, stage left.

    Supertest is a high-level http test library based on superagent, which in turn is a high-level abstraction thingy for doing http requests.

    It takes any type of server object and lets you run requests and check for expected results in a very simple manner. If need be, you can even drop down to the low-level API's for http requests.

    A simple (mocha) test might look something like this:

    var app = require("../app.js");
    describe("GET /users", function () {
    it("respond with json", function (done) {
    .set("Accept", "application/json")
    .expect("Content-Type", /json/)
    .expect(200, done);

    Where app is, for example, an express.js server - request is our test client. We are just checking that sending a GET request to /user will return some sort of json and that the status code will be 200.

    If the server isn't running when doing that require then supertest will make sure it is bound to an ephemereal port and then brought back down after the tests are done.

    Oh and because of superagent you can send any type of request and check for any type of response.


    Enhanced by Zemanta

    Did you enjoy this article?

    Published on July 23rd, 2012 in Application programming interface, Black-box testing, Hypertext Transfer Protocol, JavaScript, Node.js, Servers, Uncategorized, Unit testing, Web application

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️