Expect.js Examples Flashcards
Understand examples of chained Expect.js assertions.
expect(1).to.be.ok();
Expect the 1 to be truthy.
expect({}).to.be.ok();
Expects {} to be truthy.
expect(0).to.not.be.ok();
Expects 0 to be falsy.
expect(1).to.be(1)
Expect 1 === 1
expect(NaN).not.to.equal(NaN);
Expects NaN === NaN
expect({ a: ‘b’ }).to.eql({ a: ‘b’ });
Expects two objects to have the same key values.
expect(5).to.be.a(‘number’);
Expects a value of 5 to be of type ‘Number’
expect([ ‘dog’, ‘cat; ]).to.be.an(‘array’);
Expects value to be of type ‘Array’.
expect([ ‘dog’, ‘cat’ ] ).to.be.an(‘object’);
Expects the type object, using TypeOf Command.
expect(5).to.be.a(Number);
Expects 5 to be a Contructor Number
expect(program.version).to.match(/[0-9]+.[0-9]+.[0-9]+/);
Expects the program.version to match a regex of #.#.#.
expect([1, 2]).to.contain(1);
Expectes to find a value 1 in an array of [ 1, 2 ]
expect(‘hello world’).to.contain(‘world’);
Expects a string to ‘Hello World’ to contain the substring ‘world’.
expect([1,2,3]).to.have.length(3);
Expects an array to have three elements.
expect([]).to.be.empty();
Expects to find an array with no elements.
expect({}).to.be.empty();
Expects to find an object with no key value pairs.
expect({ my: ‘object’ }).to.not.be.empty();`
Expects an object to NOT be empty.
expect({a: ‘b’}).to.have.property(‘a’);
Expects a object to have a key of ‘a’.
expect({ a: ‘b’, c: ‘d’ }).to.only.have.keys(‘a’, ‘c’);
Expects an object to have only keys ‘a’ and ‘b’.
expect({ a: ‘b’, c: ‘d’ }).to.not.only.have.key(‘a’);
Expects an object to not only have a key of ‘a’. There must be more keys for this to be true.
expect(1).to.be.within(0, Infinity);
Expects a number that falls between the range of 1 and Infinity.
expect(5).to.be.greaterThan(3);
Expects a number (5) to be greater then a number (3).
expect(1).to.be.lessThan(3);
Expects the number (1) to be less Then the number (3).