Testing frameworks in javascript

JS testing tools are mentioned below.
  • Puppeteer
  • Cucumber JS
  • Webdriver IO
  • Cypress
  • Jest
  • Mocha

Mocha

Mocha is a unit testing framework. Main features of Mocha are
  • test framework for Node.js
  • works with any assertion library
  • run tests using mocha command. By default, it will look for tests under test directory
  • Asynchronous testing with promises
  • Test coverage reporting
  • Test retry support
  • Test duration report
  • run tests matching regular expressions
  • before and after hooks
  • run tests from command line

npm install --save-dev mocha chai mochawesome mocha-parallel-tests




var expect = require('chai').expect;
var should = require('chai').should();
var assert = require('chai').assert;
 
describe('Simple feature',function () {
      it('Check sagar length',function() {
           var len =   "sagar".length;
                  
                  
          len.should.equals(5)               
           assert.equal(len,5);
           expect(len).equals(5);

           expect(Promise.resolve('woof')).to.eventually.equal('woof');
       });

       before(function() {
           console.log("Before file")
       });
       after(function() {
           console.log("After file")
       });
       beforeEach(function() {
           console.log("Before each test")
       });
 
       afterEach(function() {
           console.log("After each test")
       });


});





//Run tests

npx mocha "xyz/**/*.js"
npx mocha-parallel-tests
npx mocha --reporter xunit --reporter-option output=report.xml

Jest

Install using npm i jest --save-devBy default, Jest expects to find test files in a folder called tests in your project folder. Create the new folder:

describe("Length function", () => {
  test("Check string length", () => {
          var len =   "sagar".length;                 
          expect(len).toBe(5);
            //return expect(fetchData()).resolves.toBe('peanut butter');

  })



//async code
test('the data is peanut butter', async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
})


});


//Run tests
npx jest

Cypress

  • Install npm i cypress
  • cypress.json file should be in root
  • Keep tests in cypress\integration
  • Run tests npx cypress run

describe("Form test", () => {
  it("Can fill the form", () => {
    cy.visit("https://www.softpost.org");
    cy.get("form");
  });
}); 

Cucumber JS

  • npm init
  • npm install @cucumber/cucumber
  • create features dir and add scenarios in it
  • Cucumber.js - config file
  • Add step def file under feature directory
  • Run tests using npx cucumber-js

const { Before, Given, When, Then } = require('@cucumber/cucumber')
let x
Given('I buy drilling tool worth ${int}', function (int) {
  x = int
});

Puppeteer

  • Puppeteer uses devtools protocol to automate chrome browser
  • Install using npm i puppeteer

   const puppeteer = require('puppeteer');

   async function run () {
       const browser = await puppeteer.launch({headless:true});
       const page = await browser.newPage();
       await page.goto("https://www.softpost.org");
       
       await page.screenshot({path: 'screenshot.png'});
       browser.close();
   }
   run();

Chai

Chai is an assertion library in JavaScript. Chai is usually used with Mocha. Main features of Chai are
  • BDD assertion style using Expect and Should
  • TDD assertion style using Assert

Jasmine

Jasmine is BDD framework for JavaScript. Main features of Jasmine are
  • BDD style tests
  • Built in assertion library
  • run tests from command line
  • comes with test doubles library

Web development and Automation testing

solutions delivered!!