4

What is the difference between chai and chai as promised in mocha framework while using protractor ?

Emna Ayadi
  • 2,430
  • 8
  • 37
  • 77

3 Answers3

6

Chai - Test Assertion Library which allows you to test your code with keywords such as expect, should etc. But while using Chai you have to take care of promises. For example

var expect = require('chai').expect;

it('should display correct tile', function() {
  var blah = 'foo';

  var title = browser.getTitle();

  return title.then(function(actualTitle) {
    expect(actualTitle).to.equal(expectedTitle);
  });
});

On the other hand if you use chai as promised then you do not need to handle promises explicitly. That could be done with the help of Chai as promised library. For example;

var chai = require('chai');
var expect = chai.expect;

var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

it('should display correct title', function() {

  var actualTitle = browser.getTitle();

  return expect(actualTitle).to.eventually.equal(expectedTitle);
});
Priyanshu
  • 3,040
  • 3
  • 27
  • 34
3

Chai is a BDD assertion library - providing you with common keywords such as assert or should etc.

Chai as Promised is an extension of that library specifically made to handle assertions with promises (as opposed to resolving them manually yourself).

https://github.com/domenic/chai-as-promised/

Gunderson
  • 3,263
  • 2
  • 16
  • 34
1

It helps you test code that uses Promises. These can be tricky to test since they rely on callbacks to be executed and are generally async. This writeup might help you: http://www.sitepoint.com/promises-in-javascript-unit-tests-the-definitive-guide/

pherris
  • 17,195
  • 8
  • 42
  • 58