Blog posts tagged 'node.js'

Stubbing middleware when testing an Express with Supertest

Jun 25 15

When testing a simple express app / api I like to use supertest.

This can feel like integration testing, and to an extent it is.
But we can stub things out with sinon, and tidy up our tests.

Let’s look at an example.

Suppose we have a simple API with the following router:

To run the app, we have a separate server.js:

This makes loading our app much easier in our supertest tests, as we’ll see later on.

Ok, nothing complex going on here –
We can see we have some authentication middleware on all of our routes:

This is some very basic authentication, for the purposes of our demo:

Ok, all is good, our app runs, and if we don’t supply an authentication token in the header, we’re returned a 401, otherwise, we’re presented with ‘Hello’

Our tests currently look like this:
(see the full test at this version here)

These all pass


The testing for with / without authentication header set stuff bothers me;
For a start, it’s a cross cutting concern – it shouldn’t be tested like this.
Plus – we’re integration testing our authentication middleware.
What happens if/when we make this more complex?

Much better would be to stub this out.

So, by changing our test slightly as follows:

We can create a stub of our ensureAuthenticated middleware.

Important here, is to note the removal of the “with authentication header set” tests… we don’t need them anymore! That’s taken care of by

Hope this helps!

I’ve added the full code on GitHub: GitHub

Transparent proxy from node.js app to WordPress for /blog route

May 1 15

For a recent car wash directory start up I have created, I wanted a blog.
GetCarClean uses node.js;
While there are node blogging platforms, what I really wanted to do, was proxy /blog of my main site, to a WordPress site (actually hosted on a subdomain – blog.<domain>.com)
What I did, was to set up the WordPress blog on the separate server, ensuring my styles etc… are in my ‘main’ site css
Then, in my Express route, I used the express-http-proxy module, and configured it as follows:

This now transparently proxies from /blog to the subdomain!

Getting header / footer content from separate resource

I created a separate route for the header & footer of the site, so that these always stay ‘in sync’ with the main site.
This stops the need to update these sections in WordPress – I can simply leave WordPress to manage my blog posts.

To pull in the content, I use the following (footer.php example)

This retrieves the content from the provided url, and spits it out in my template.

Redirecting to /blog from .blog subdomain

One thing I am currently stuck on;
If a user visits – I want them to be redirected to

I’ve got a htaccess set up for this, and I”m almost there.

Please see this StackOverflow question for more details:

WordPress Settings

Below, are the settings I used for the WordPress URLs, to ensure URLs are re-written correctly

Proxy blog nodejs wordpress settings

Introducing mongoose-status-manager – status updates for mongoose models

Sep 26 14

For a project I’m currently working on, I needed to be able to update the status of a document.
For example, let’s assume it’s an Order.

I need to create it, then set it’s status to ‘Pending’
When the Payment is completed, I need to set it to ‘Paid’
Then, as the order progresses, I need to set it to ‘Packed’, finally ‘Dispatched’

I also need to keep a history of status updates, for audit / logging reasons.

So, I created Mongoose Status Manager

This makes it as easy as:

It’s important to note that changes aren’t persisted until .save() is called.

Full documentation is available on the README on the github page

Available on github

How to assert.throws on an async function with a callback

Apr 1 14

Recently, I was writing a module that took a callback, and needed to write a test, asserting on an exception.

Here’s how to do it:

Assuming our module looks something like this:

Create a standalone EXE to run a Node.js application

Jul 19 12

Download Advanced BAT to EXE converter

Download Node.exe from Joyent

Now we’re ready to bundle the node.js executable and our js files into a single exe file.

As for our BAT command, all we need is:

BAT Command

node test.js

%MYFILES% is the variable given to the location of embedded files (more on that in a sec)

Your Advanced BAT to EXE converter window should look like the image on the left.

Don’t worry about bundling the files together just yet, we’ll do that in a sec.

For the purposes of this demo, I’ve created a very simple test.js file, that looks like this:

Save that .js file locally.

Now, from the File menu, select Build Exe (CTRL+ B)

Enter a filename for the final exe file.

EXE Options

Next, the EXE options pane will be displayed.

Click the Embed Files tab, and select both your node.exe and test.js files.

Set any other options you feel you’ll need here.

Then, simply click Build EXE.

Your executable will be bundled and in the location you chose to save it.

Run it, and you’ll have 1-10000 outputted into the console.