Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai Test Suites and Test Specs (describe and it)

Brian Patterson
Brian Patterson
19,588 Points

can someone explain to me expect(true).to.be.ok

I am a little mystified with the function.

//Test suite
describe('Mocha', function () {
    //Test spec (unit test)
    //first argument is a string describing the behaviour
    it('should run our tests using npm', function() {
        //state condition 
        expect(true).to.be.ok;
    });
});

How does it know that we are testing for Mocha? I am still confused about the expect(true).to.be.ok; What does that mean in layman terms!!

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

http://www.chaijs.com/api/bdd/#method_ok

the expect was imported from chai. the ok method means it would return a truthy value (not: null, NaN, 0, undefined, false, empty string), which would satisfy an if statement.

Kenneth Baur
Kenneth Baur
12,400 Points

From chai's mdn

"...you chain together natural language assertions."

The 'natural language' part really helps (it helps me grasp it) to understand how chai works. You write is though you are talking. That's it!

to.be.ok is checking that a value is...ok, that is, if it is a true value (truthy). While it is not a === strict equality, it's used for loose comparisons.