Create and execute unit tests within minutes using Jest Framework
Unit Tests builder
is a testing toolkit developed on the top of Facebook's Jest framework for the developers and testers to seamlessly write backend unit tests for every scenarios.
Installation
npm i jest-builder --save
Configuration
const testApp = require('jest-builder');
const test = new testApp({<--config-->})
Setting up your config
Property | Description | Default Value | Type |
---|---|---|---|
file_path |
Excel File Path | UnitTest.xlsx | string |
jest_flags |
Jest CLI Flags (https://jestjs.io/docs/cli) | ["--passWithNoTests", "--coverage"] | array of string |
suite_name |
Name of the test suite | Unit Test Suite | string |
html_report |
Test case execution report in HTML format | false | boolean |
excel_report |
Test case execution report in Excel format | false | boolean |
text_report |
Test case execution report in Text format | false | boolean |
mail_report |
Send an email using Nodemailer SMTP Service | {} | object |
Steps
- Import and initialize the package
- Add configuration (
if required
) - Create an excel file (
if required
) - Add the unit tests (Each row will be considered as a single unit test)
- Execute the code
Excel Columns
Test Case Name | Data | Expected Data | Validation | Function | Path | Skip Tests |
---|
- Test Case Name - Name of the Test Case
- Data - Data that has to be provided to the function (Given Data)
- Expected Data - Data that is expected after successful completion of test case or the data that is to be passed to the validations.
- Validation - Jest validation expect functions. (For more: https://jestjs.io/docs/expect)
- Function - Name of the function (without parentheses) that is to be executed for a specific test case.
- Path - Relative path of the file where the function is declared.
- Skip Tests - It is boolean or a Yes/No value. Yes/true will skip that test case and No/false will run it.
Note: (1) All the above columns are mandatory with the same name (2) To use multiple validations in one unit test use '/' as a delimeter.
eg: toBe/toEqual
(3) If you're not specifying the parameters in jest validation functions, it will take Expected Data as a parameter. e.g: If theExpected Data
is"Login Success"
andValidation
istoBe
, thentoBe
function will take"Login Success"
as a parameter. Similarly, if you can also pass the parameters inside the validation functioneg: toBeGreaterThan(20)
Example
Without configuration
After installing the package, create app.js
file and initialize the test application.
const testApp = require('jest-builder');
const test = new testApp({})
Execute the code.
node app.js
Note:
As the config is empty it will create a dummy test file named
UnitTest.xlsx
in the root folder and execute the demo test written inside it. It can be used as a future reference as well
With configuration
After installing the package, create app.js
file and initialize the test application (Now with a configuration)
const testApp = require('jest-builder');
const test = new testApp({
"file_path" : "./UT_Manual.xlsx"
})
Create a controller file Login.js
module.exports.login = function (data) {
if (data.username == 'xyz.abc')
return "Login Success"
else
return "Login Failure"
}
Create a excel file UT_Manual.xlsx
with the following tests.
Test Case Name | Data | Expected Data | Validation | Function | Path |
---|---|---|---|---|---|
Login with Valid Data | {"username": "xyz.abc"} | Login Success | toBe | login | ./Login.js |
Login with invalid Data | {"username": "abc.xyz"} | Login Failure | toBe/toEqual | login | ./Login.js |
Execute the code.
node app.js
Result
Unit Tests
√ Login with Valid Data (10 ms)
√ Login with invalid Data (10 ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.781 s, estimated 1 s
Generate reports
3
types of reports are available in this toolkit i.e html, excel and text
app.js
const testApp = require('jest-builder');
const test = new testApp({
"file_path" : "./UT_Manual.xlsx",
"html_report": true,
"excel_report": true,
"text_report": true
})
Note:
HTML
,Excel
andText
reports are generated inhtml-report
folder,report.xlsx
file andtests
folder respectivly.- To generate excel report,
excel_report
andtext_report
property must betrue
.
Setting up Jest Flags
Jest provides variey of CLI flags which can also be passed in the configuration.
app.js
const testApp = require('jest-builder');
const test = new testApp({
"jest_flags": ["--silent", "--showConfig"]
})
Note:
For more Jest CLI flags, follow https://jestjs.io/docs/cli
Send the reporting email
This toolkit also provides a feature of sending an test reporting email.
app.js
const testApp = require('jest-builder');
const test = new testApp({
"mail_report": {
"send_mail": true/false //Must be true if you want to activate the reporting email,
"reports": ["html", "excel", "text"],
"smtp_config": {
"host": "<--SMTP Host-->",
"port": 1234,
"secure": true/false,
"auth": {
"user": "<--User Name-->",
"pass": "<--Password-->",
}
},
"mail_options": {
from: "<--From Email Address-->",
to: "<--To Email Address(1), To Email Address(2)...,To Email Address(n)-->",
subject: "<--Email Subject-->",
html: "<--Email HTML Body-->",
attachments: [],
}
}
})
For more details, follow https://jestjs.io/docs
For queries, reach out to thakkarjammy23495@gmail.com (Jenil Thakkar)