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()
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).
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.
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.
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.
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.
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.
#!/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);
});
});
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));
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With