-
Notifications
You must be signed in to change notification settings - Fork 0
Chai
Koen Verheyen edited this page Sep 19, 2017
·
1 revision
Chai is a library that adds assertions to your tests.
Chai defines 3 different "flavors" to write assertions. These are all available in Cypress out of the box. However, most Cypress tests are written asynchronously which means assertions have to be written in callback functions which may make your code less readable. Therefore Cypress added the should
method to simplify asserting asynchronously.
In the following examples, consider this setup:
let foo = 'bar';
let tea = { flavors: ['mint', 'lemon', 'jasmine'] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(tea).to.have.property('flavors').with.lengthOf(3);
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
tea.should.have.property('flavors').with.lengthOf(3);
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);
cy.wrap(foo)
.should('be.a', 'string')
.should('equal', 'bar')
.should('have.length', 3);
cy.wrap(tea)
.should('have.property', 'flavors') // Chain subject changes at this point
.should('have.length', 3);
Notice how some of the should
assertions change the subject of the Cypress chain. In the first statement, the wrapped variable foo
remains the subject and all assertions are executed against foo
. In the latter example, the first should
method changes the subject from tea
, to tea.flavors
.