Testing the server

Testing the server.js file will be slightly different than any of our other files. The file runs as the root of our application, so it doesn't export a module or any object that we can directly test. Since we launch our server using server.js, we will need to emulate launching our server from our code. We'll create a function called server, which will require the server.js file using proxyquire, and stub each of the modules that it requires. Executing the server() function will be exactly the same as executing node server.js from a command line. All of the code within the file will execute via that function, and then we can test against each of the calls that are made using stubs from within proxyquire.

Create a file named server.test.js within the tests/server/ folder and insert the following block of code:

let proxyquire, expressStub, configStub, mongooseStub, app, 
server = function() {
proxyquire('../../server', {
'express': expressStub,
'./server/configure': configStub,
'mongoose': mongooseStub
});
};

describe('Server',()=>{
beforeEach(()=>{
proxyquire = require('proxyquire'),
app = {
set: sinon.spy(),
get: sinon.stub().returns(3300),
listen: sinon.spy()
},
expressStub = sinon.stub().returns(app),
configStub = sinon.stub().returns(app),
mongooseStub = {
connect: sinon.spy(),
connection: {
on: sinon.spy()
}
};

delete process.env.PORT;
});

// to do: write tests...
});

Before each test is run for our server, we reset the stubs for all of the major components of the server. These stubs include the app object, express, config, and mongoose. We're stubbing each of these modules since we want to spy on them (and we use a stub because some of them need to return objects that we'll work with in our file). Now that we have all of our spies in place and our app object scaffold set up, we can start testing the main functionalities of our code.

We will need to check whether the following conditions pass:

An application is created

  • The views directory is set
  • The port is set and can be configured and/or set to default
  • The app itself is configured (config is called with it)
  • Mongoose connects to a database URI string
  • The app itself is launched

Replace the // to do: write tests... comment in the earlier code with the following block of code:

describe('Bootstrapping', ()=>{
it('should create the app', ()=>{
server();
expect(expressStub).to.be.called;
});
it('should set the views', ()=>{
server();
expect(app.set.secondCall.args[0]).to.equal('views');
});
it('should configure the app', ()=>{
server();
expect(configStub).to.be.calledWith(app);
});
it('should connect with mongoose', ()=>{
server();
expect(mongooseStub.connect).to.be.calledWith(sinon.match.string);
});
it('should launch the app', ()=>{
server();
expect(app.get).to.be.calledWith('port');
expect(app.listen).to.be.calledWith(3300, sinon.match.func);
});
});

In the preceding group of tests, we test the bootstrapping of our server, which are all of the functionalities that initially run within server.js. The names of the tests are pretty self-explanatory. We're checking against the various methods of the app object, ensuring that they're called and/or that the correct parameters were passed in. For the tests, we want to test that a specific type of parameter was called, not what the parameter value was literally; we use Sinon's match element, which allows our tests to be a little more generic. We wouldn't want to hardcode the MongoDB URI string in our tests, because that's just another place we would have to maintain--although you could very well do this if you wanted your test to be that strict (that is, to assert that the exact URI string was passed quite literally).

In the second set of tests, we want to ensure that the port is set, that it defaults to 3300, and that it can be changed via the use of a node environment variable:

describe('Port', ()=>{
it('should be set', ()=>{
server();
expect(app.set.firstCall.args[0]).to.equal('port');
});
it('should default to 3300', ()=>{
server();
expect(app.set.firstCall.args[1]).to.equal(3300);
});
it('should be configurable', ()=>{
process.env.PORT = '5500';
server();
expect(app.set.firstCall.args[1]).to.equal('5500');
});
});

With these tests in place, run the npm test command again, and you should get the following output:

    $ npm test           
Server
Bootstrapping
should create the app (364ms)
should set the views
should configure the app
should connect with mongoose
should launch the app
Port
should be set
should default to 3300
should be configurable
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.50