Mocha: Mocha --require option only available through cli?

Created on 22 Dec 2014  ·  16Comments  ·  Source: mochajs/mocha

Is it possible to access the require option through the programmatic api?

ie:

var Mocha = require('mocha');

new Mocha({
    require: ['should']
}).run(...)
question

All 16 comments

Please use the Google group for questions.

The answer is no, but it's likely not necessary and you're doing something weird. Please continue discussion on group.

hey @DylanPiercey @boneskull github is actually the right spot, don't use the google group.

@DylanPiercey you want to do this to avoid requiring should at the top of your tests?

@travisjeffery No, actually I am trying to use 'co-mocha' with 'protractor'.

However internally protractor uses the programmatic api for mocha and as such there is no way to inject such requires (That I know of).

Any plans to implement this? I'm also working on my own project that uses the programmatic API.

@TylorS Could you please give a bit of background around your use-case? Maybe this would hint an implementation idea or an acceptable work-around. 🤓

Hi @gurdiga I'd love to share my use-case. I'm working on a project that makes it easy to start new node/npm projects with virtually 0 configuration. Its very much at the proof of concept level of things. It works by using plugins to configure your needs.

At the moment I only have a mocha plugin with some basic plugins to augment mocha. Each plugin for mocha receives a mocha instance and as a workaround to this issue its passed require() from the same scope as the mocha instance. You can see it here. I'm not a fan of the require hack, and would love to be able to do something more 'official' like mocha.register() if technically feasible.

Please let me know if there is any more information I can provide to make things more clear or how I can help in any way.

Anyways here is a link to it if you're interested https://gihtub.com/TylorS/bubbleup and a basic example of the test functionality which uses mocha, buba (a babel-register alternative), and istanbul https://github.com/TylorS/bubbleup-test-example.

Hey @TylorS, 🙂

It looks like Mocha’s CLI code is employing the “require hack” too: I mean it’s just collecting a list of names/paths to later just require them. 🤓

I’ve paused then because I didn’t know what to make of it. 🙃  — Then I’ve re-read the documentation for the --require option and it says:

Note that this works well for should as it augments Object.prototype, however if you wish to access a module’s exports you will have to require them, for example var should = require('should').

So it seems it was not meant to be part of the Mocha core, and is consequently why it only lives in the CLI code. I guess this means there will be resistance to implementing it as part of the programatic API, but this is only my guess as an outsider.

Now, if bubbleup-plugin-test-mocha will need to access the modules’ exports — which is not offered by the Mocha CLI’s implementation — I would probably consider implementing your own mechanism in bubbleup. 🤓  And if I think about it, it seems to me that a tool like bubbleup is a better home for this kind of functionality than Mocha — a testing tool — is. 🤔

I’m not sure I have helped, but this is all I have. 😊

Thank you @gurdiga for taking the time to investigate this. I simply mean to use it for things like babel-register which allow writing tests in ES6+. I'm not sure if that makes any difference.

I feel better about the solution I have come to now after seeing that mocha also uses my "require hack".

Thank you very much for your time :)

Hello, I was looking for this feature too and I came to this discussion. My situation is that I have a main script that instantiates Mocha programmatically and use addFile() function to add unit tests that are located in different places on the file system.
All of these files use Chai Assertion Library and I would avoid to add require("chai") on every one of them. Above all, for many reasons, I have to install Chai locally on every path and this is not so convenient.

At the end, the "require" option on Mocha can be the solution. I'm relatively new on NodeJs so maybe @TylorS could explain me his hack... or I'll just wait for someone to let us use require option programmatically.

Hello @alessandrofrancesconi This is only off the top of my head, as I only did this one time. It ended up looking something like this

const Mocha = require('mocha')

const mocha = new Mocha()

require('babel-core/register') // for instance, needs to be done *before* you call addFile

testFiles.forEach(file => {
  mocha.addFile(file)
})

mocha.run()

I hope this helps!

Mh, I'm not seeing any improvement, @TylorS . Here is an example:

Main file with Mocha initialization:

// initialize Mocha test framework
var Mocha = require("mocha");
var runner = new Mocha(
{
    ui: "bdd"
});

require("chai");     // I put here the modules needed by external files
require("sleep");

// process all test scripts in path
var fsFiles = fs.readdirSync(args.path);
fsFiles.forEach(function(f) 
{
    runner.addFile(path.join(args.path, f));
});

// run mocha
runner.run();

And here is an example of file loaded with addFile()

describe("Initialization test", function () {
    it("shall initialize without errors", function () {
        chai.expect(0).to.be.below(1, "it works");
    });
});

Here, "chai" is not defined (but describe() and it() work)

Oh, I don't know how that example would work. I don't think that --require works that way at all? (Someone please correct me if I'm wrong).

In my example this workaround happens to work, because babel-core/register mutates the way the following .js files are require()d. I can't see how you'd be able to add anything globally without doing so yourself.

-require("chai");
-require("sleep");

+global.chai = require('chai');
+global.sleep = require('sleep');

You're right, now it works 👍

Glad that I could help @alessandrofrancesconi

Question answered and resolved.

Was this page helpful?
0 / 5 - 0 ratings