What is the difference between chai and chai as promised in mocha framework while using protractor ?
3 Answers
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);
});

- 3,040
- 3
- 27
- 34
-
Thanks a lot for the example. – Emna Ayadi Apr 26 '16 at 07:45
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).

- 3,263
- 2
- 16
- 34
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/

- 17,195
- 8
- 42
- 58
-
Thanks for your answer, So to test an angular js app is it better to use jasmine or Mocha framework with protractor? – Emna Ayadi Apr 26 '16 at 08:05
-
1@Emna The Protractor docs recommend Jasmine, though both are supported. – Gunderson Apr 26 '16 at 10:31
-
@Gunderson Yes this what i'm searching what is recommended, thanks :) – Emna Ayadi Apr 26 '16 at 10:42