Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha as a library

I would like to use mocha (the node.js test framework, not the ruby mocking library) as a library, rather than using the mocha executable to run my test.

Is it possible to run a mocha test this way? The examples all just call mocha libraries assuming they are already "require'd", and the mocha executable does all the "require-ing" ahead of time, but I would really prefer to do them explicitly in my script so that I can simply set +x on my script and call it directly.

Can I do something like this?

#!/usr/bin/env coffee
mocha = require 'mocha'
test = mocha.Test
suite = mocha.Suite
assert = require("chai").assert

thing = null

suite "Logging", () ->
  setup (done) ->
    thing = new Thing()
    done()
  test "the thing does a thing.", (done) ->
    thing.doThing () ->
      assert.equal thing.numThingsDone, 1
      done()
  teardown (done) ->
    thing = null
    done()
like image 915
Eric Hartford Avatar asked Mar 13 '12 21:03

Eric Hartford


People also ask

Is Mocha a library?

Mocha is a testing library for Node. js, created to be a simple, extensible, and fast. It's used for unit and integration testing, and it's a great candidate for BDD (Behavior Driven Development).

Is Mocha an open-source?

Mocha is an open source JavaScript testing framework that runs on Node. js and in the browser. It's designed for testing both synchronous and asynchronous code with a very simple interface.

What is Mocha and chai?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.

Which is better jest or Mocha?

Jest is also faster than Mocha. It has built-in support for snapshot testing, which means tests are run automatically on each change to the code. This makes it easy to keep your tests up to date as you work. Mocha has more features out of the box since it is a more mature tool with a larger community of contributors.


2 Answers

It's possible, but certainly not recommended.

Look at the mocha binary's source (specifically bin/_mocha) to get an idea of what it does. In particular, look at the run function. Everything it's using—Runner, Reporter, etc.—is exported by the mocha library, so there's nothing stopping you from reverse-engineering it.

like image 55
Trevor Burnham Avatar answered Oct 04 '22 21:10

Trevor Burnham


This feature has since been added. I include an example below.

I got information from here:

You will need 2 files. One test, and one to run the test. You can mark runTest as executable, and set its output in the mocha options.

runTest.js

#!/usr/bin/env node

var Mocha = require('mocha'),
    fs    = require('fs'),
    path  = require('path');

var mocha = new Mocha(
{
  ui: 'tdd'     
});

mocha.addFile(
  path.join(__dirname, 'test.js')
);

mocha.run(function(failures){
  process.on('exit', function () {
    process.exit(failures);
  });
});

test.js

var assert = require('chai').assert

suite('Array', function(){
  setup(function(){});
  suite('#indexOf()', function(){
    test('should return -1 when not present', function(){
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});
like image 24
Eric Hartford Avatar answered Oct 04 '22 21:10

Eric Hartford