Automating everything

Test automation was never so easy without Gulp. Gulp is an open source JavaScript library that provides efficient build creation processes and serves as a task runner. We will use Gulp to automate our unit tests by a single command in terminal.

Let's start by installing all the required packages using the following commands:

npm install gulp-cli -g
npm install gulp --save-dev
touch test/gulpfile.js
gulp --help

Note that you may require root access to install the global package of gulp-cli. Use sudo in such cases, for example, sudo npm install gulp-cli -g. We used --save-dev to install Gulp locally and save it as a development dependency in our package.json.

Further, we created a Gulp file in the test directory. Now, to test the directory of our app and make sure that we have the following file structure:

Once we've installed the required packages and created a Gulp file, let's start writing some code, as follows:

var gulp = require('gulp');
gulp.task('default', function() {
console.log("Lets start the automation!")
});

Go back to the terminal, run Gulp, and you will receive the following output:

Gulp is faster and simpler; why?: Gulp uses node.js streams to pass a data chunk through a series of the piped plugin. This accelerates the in-memory process operations and performs the write operation at the end of the task, once and for all.

Let's get ready to expand the scope of learning Gulp and automate our unit tests we covered in the previous sections. We will start by installing the other required npm packages. Note that we will need to install them in our project folder and not in the test folder. So, let's go back one step using cd.., and ensure that you are at your project's root level and run the following command:

npm install gulp-mocha --save-dev

The gulp-mocha is a plugin to run the mocha test files. Now, let's modify our Gulp file and add some es6 spices, as follows:

const gulp = require('gulp');
const gulpMocha = require('gulp-mocha')
gulp.task('test-helper',()=>gulp.src('./testhelper.js'))
gulp.task('test-server', ['test-helper'],()=>{
return gulp.src('./server/server.test.js')
.pipe(gulpMocha())
});

Now, run gulp test-server to get the following output:

Let's discuss how the preceding code works. First, we created the test-helper task that reads the testhelper.js file in memory and does nothing else but store the global variables required to test our server.test.js.

We used mocha as a framework to write test cases. The gulpMocha plugin runs the test cases in memory by reading the server.test.js file and pipes the output in terminal. To study about gulp-mocha in detail, follow the https://github.com/sindresorhus/gulp-mocha link.

Note the syntactical structure of how to write dependencies if required. Let's get clarity on adding a dependency by writing one more task, as follows :

gulp.task('test-routes', ['test-helper', 'test-server'],()=>{
return gulp.src('./server/routes.test.js')
.pipe(gulpMocha())
});

This time we will run gulp test-routes.

Now, a question may arise regarding managing so many tasks. Gulp also provides a solution for automating all the tasks in one go. Let's add the following snippet to our file:

gulp.task('build', ['test-helper', 'test-server','test-routes'])

Run the preceding command; Gulp build and automation of unit tests are both now complete. Further, we can add all the controllers and respective models so as to achieve project-based automation of test cases.

..................Content has been hidden....................

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