What's the difference between assertion library, testing framework and testing environment in javascript?

JavascriptUnit TestingTest Framework

Javascript Problem Overview


Chai is an assertion library.

Mocha and Jasmine are testing frameworks.

and Karma is a testing environment.

I've already read Difference between available testing frameworks: mocha, chai, karma, jasmine, should.js etc.

Javascript Solutions


Solution 1 - Javascript

Assertion libraries are tools to verify that things are correct.
This makes it a lot easier to test your code, so you don't have to do thousands of if statements.
Example (using should.js and Node.js assert module):

var output = mycode.doSomething();
output.should.equal('bacon'); //should.js
assert.eq(output, 'bacon'); //node.js assert

// The alternative being:
var output = mycode.doSomething();
if (output !== 'bacon') {
  throw new Error('expected output to be "bacon", got '+output);
}

Testing frameworks are used to organize and execute tests.
Mocha and Jasmine are two popular choices (and they're actually kinda similar).
Example (using mocha with should.js here):

describe('mycode.doSomething', function() {
  it ('should work', function() {
    var output = mycode.doSomething();
    output.should.equal('bacon');     
  });
  it ('should fail on an input', function() {
    var output = mycode.doSomething('a input');
    output.should.be.an.Error;
  });
});

Testing Environments are the places where you run your tests.

Karma is a bit of an edge case, in the sense that it's kind of a one off tool, not many like it. Karma works by running your unit tests inside of browsers (defaulting to PhantomJS, a headless WebKit browser), to allow you to test browser-based JavaScript code.

Frameworks like Mocha and Jasmine work both in the browser and with Node.js, and usually default to Node.

Solution 2 - Javascript

The testing environment (or test runner) is what runs all of your tests. It launches them, aggregates results, etc.

The testing framework is what you use to create each of the tests. For example, jasmine uses a syntax of

it('name of test', function() {
   // do some tests
});

The assertion library is what does the actual verification of your test results

it('name of test', function() {
   assert x == 5 //pseudocode, the syntax will vary based on your asserting framework 
});

Solution 3 - Javascript

Taking a shot at a simpler answer. I'm a noob, but here's what it sounds like.

Mocha organizes the tests, and is where you start tests. There's basic "assertion" in nodeJS that you can use to test things act as expected.

Chai is a way to extend the "assertion" framework so you can write more semantically helpful things like:

expect(foo).to.be.a('string');

instead of a less explicit style:

assert.ok(typeOf(foo) ==='string')

Or something with less semantic context.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaderView Question on Stackoverflow
Solution 1 - JavascriptZoey MertesView Answer on Stackoverflow
Solution 2 - JavascriptJeff StoreyView Answer on Stackoverflow
Solution 3 - Javascriptjsilv21View Answer on Stackoverflow