We’ll start with creating a simple Node.js application, just like we usually do. The subsequent part of this article will explain how to write, run, and automate tests with BuddyWorks.
If that’s the first time you work with Node.js, install the npm manager first: nodejs.org/en/download/package-manager
Create a directory for the application:
mkdir myapp && cd myapp
Now, npm should be initialized. We’ll use it in order to create a package.json
with the Mocha framework:
npm init
You will be asked for the application’s details. Type the following:
hello-world
app.js
./node_modules/.bin/mocha
This framework will be used for testing the applicationConfirm the rest of the values by hitting enter.
Express Node.js web application framework will be used for building the app:
npm install express --save
Once you already have everything installed, let’s create an app.js
file with a simple HTTP server that will serve our Hello World website:
//Load express module with `require` directivevar express = require('express')var app = express()
//Define request response in root URL (/)app.get('/', function (req, res) { res.send('Hello World')})
//Launch listening server on port 8080app.listen(8080, function () { console.log('App listening on port 8080!')})
Now, the application is ready to launch:
$ node app.js
Go to: http://localhost:8080/
in your browser to view it.
Each single application should be tested before the deployment to the server, especially, if it’s a welcome site that creates the first impression. Here, we will use Mocha as the test running framework, and Chai as the assertion library.
Make sure to add Mocha and Chai packages to the package.json
:
npm install mocha --savenpm install chai --save
Now, it’s time for defining our first test…