DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Blackbox Testing node.js Apps

Blackbox Testing node.js Apps

Swizec Teller user avatar by
Swizec Teller
·
Aug. 21, 12 · Interview
Like (0)
Save
Tweet
Share
3.28K Views

Join the DZone community and get the full member experience.

Join For Free

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

Blackbox testing is my favourite kind of testing. It is 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){
    request(app)
      .get('/user')
      .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.

Awesome.

app unit test Blackbox Node.js Requests

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Check Docker Images for Vulnerabilities
  • How Observability Is Redefining Developer Roles
  • What Is a Kubernetes CI/CD Pipeline?
  • Mr. Over, the Engineer [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: