简体   繁体   中英

How can I get started with mocha and make my first test?

I want to figure out whats wrong with my JS code. The code is pretty simple, and has a really simple error, but I want to set up tests. I googled around for javascript testing frameworks and found one similar to RSPEC, called Mocha, which has a Chai extension which I think gives it more jQuery functionality.

I'm not a nodejs user, I'm usually a PHP backend with some Ruby on Rails experience, So npm install mocha intimidates me. I couldn't find anything on google that suggested I could get mocha set up without node package manager, and it generally seemed really hard to set up my first test. I don't want to spend so much time familiarizing myself with node, when all I want to do is write a simple test and try testing out.

Actually, "installing" mocha is not that much more complicated than "installing" jQuery! Simply include the file. This makes it actually really easy to set up a JSFiddle for testing Javascript code. I've done that with 3 simple tests so that you can see the syntax, and easily get started testing your javascript code with mocha. As always, refer to the mocha docs , the chai docs when you want to learn more.

mocha.setup("bdd");
chai.should();
var asdf = "tests.";
//simple test
describe('Simple ' + asdf, function(){
    it('true = true?', function(){
        var j = true;
        var i = true;
        i.should.equal(j);
    });
});
// Run all our test suites.  Only necessary in the browser.
mocha.run();

Here is a JSFiddle with mocha loaded so you can get started:

http://jsfiddle.net/kbjwLsab/12/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM