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

3

75

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.

Kola answered 5/9, 2014 at 3:3 Comment(2)
To make things even more complex, there are also Selenium Webdriver Node.JS clients, some of which let you run Jasmine, Cucumber or Moca+Chai tests within them (e.g. WebdriverIO).Threefold
The question is not well written given that the OP gives examples of the 3 things they want clarification on, and they have even given a link for more info. Its almost its own answer to the question title. However, I am 'assuming' that given the info provided, the OP wants a more detailed breakdown. But it would help to know what parts are still unclear to the OP.Simmonds
A
70

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.

Angulo answered 5/9, 2014 at 3:30 Comment(3)
Sorry I still dont get it. both testing framework and test runner can run my test. why we need karma??Kola
@Kola Karma makes it a lot easier to test browser-based JavaScript code, otherwise you're basically stuck with running the tests in Node.js. While Node's fine for a lot of things, it doesn't work so well for browser-based code.Angulo
I like your example of an assertion in vanilla JS. It's worth emphasizing that you can write unit tests without an assertion library. Assertion library's like should.js, ChaiJS, or Node.js Assert are mere syntactic sugar because they provide more human readable tests by allowing you to write assertions in a natural language syntax.Luzern
V
55

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 
});
Vergievergil answered 5/9, 2014 at 3:9 Comment(1)
Note that the testing framework can also aggregate results. E.g. mocha has several different reporters.Madera
B
0

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.

Beaker answered 27/2, 2022 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.