Project to create automation tests of APIs with Mocha bringing BDD to JavaScript.
Although this project works fine with NPM we recommend to use Yarn (>= 0.20.0) instead, due to its speed & solid dependency locking mechanism. To keep things simple we use yarn in this guide, but feel free to replace this with NPM if that is what you are using.
Choose one of the following options:
Download the latest stable release or clone the git repo — git clone repoName
Then:
/api_automation_boilerplate-javascript
yarn install
) or (npm install
)Now you are ready to write your own tests.
Tests are written in Mocha BDD syntax
myFirstTest.js
describe('My First test', () => {
let app = 'My first test';
it('should pass', () => {
app.should.equal('My first test')
});
});
Traverse to the project directory
$ cd api_automation_boilerplate-javascript
On one tab run faker api(Using the json-server module we are creating fake api for testing purpose) :
$ npm run fakeApi
In another tab execute the test run using :
$ npm test
To configure your tests, checkout the [setup.js
] file in your test directory.
You can setup multiple configs for specific environments. Let's say you want to have a different baseUrl
for
your local and pre-deploy tests. Use the setup
to set all general configs (in mochaOpts) that don't change.
They act as default values. For each different environment you can create a new setup with the following name
scheme:
setup.<ENVIRONMENT>.js
and then accordingly require it in your mocha.opts file using
--require ./test/setup.js
Sometimes its useful to only execute a single test file, to do so use the following command:
$ npm test --grep nameOfSuite (nameOfSuite is the name of describe or it block)
If you have failing or unimplemented tests you can mark them as "skip" so they will get skipped.
describe.skip();
it.skip();
To assert values this project comes with a Chai integration.
Winston is used as logger in the project. Use the below command to get logs on console while running tests-
logLevel='debug' npm test
Supported log levels are-
{ error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
To use logger, use any of the below format with respective logLevels-
// Default logger
winston.log('info', "127.0.0.1 - there's no place like home");
winston.info("127.0.0.1 - there's no place like home");
To use winston's inbuilt logger module(project uses winston directly), make the configuration as -
var winston = require('winston');
var logger = new (winston.Logger)({
levels: {
trace: 9,
input: 8,
verbose: 7,
prompt: 6,
debug: 5,
info: 4,
data: 3,
help: 2,
warn: 1,
error: 0
},
colors: {
trace: 'magenta',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
debug: 'blue',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
error: 'red'
}
});
logger.add(winston.transports.Console, {
level: 'trace',
prettyPrint: true,
colorize: true,
silent: false,
timestamp: false
});
logger.add(winston.transports.File, {
prettyPrint: false,
level: 'info',
silent: false,
colorize: true,
timestamp: true,
filename: './nKindler.log',
maxsize: 40000,
maxFiles: 10,
json: false
});
The project uses two reporters-
npm run report
Use of more than one reporter has been made possible by using mocha-multi
The project uses apiDoc to document the Apis. To generate and see the api documentation use-
npm run generateDocument