Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

mocha.opts is not processed properly #88

Closed
ghost opened this issue Mar 6, 2016 · 2 comments
Closed

mocha.opts is not processed properly #88

ghost opened this issue Mar 6, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Mar 6, 2016

I wan't to specify which files are my test files with the following pattern: test/**/*spec.js. Since I run my tests with nodejs using mocha from command line and using karma start tdd.karma.conf.js --single-run, I thought it would be best to put this info into the mocha.opts file, so it would be loaded by both the nodejs version and the browserified version, which is used by karma.

I use karma with the following config file currently:

module.exports = function (config) {
    config.set({
        plugins: [
            "karma-browserify",
            "karma-phantomjs-launcher",
            "karma-mocha"
        ],
        frameworks: ["browserify", "mocha"],
        files: [
            "index.js",
            "src/**/*",
            "test/**/*"
        ],
        preprocessors: {
            "index.js": ["browserify"],
            "src/**/*.js": ["browserify"],
            "test/**/*.js": ["browserify"]
        },
        client: {
            mocha: {
                reporter: "html",
                ui: "bdd"
            }
        },
        browserify: {
            debug: true
        },
        browsers: ["PhantomJS"],
        reporters: ["progress"],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        captureTimeout: 6000,
        singleRun: false
    });
};

So by starting the karma server with karma start tdd.karma.conf.js I will be able to develop and run the tests automatically with watch and by starting with karma start tdd.karma.conf.js --single-run I will be able to automatically test in a pre-commit hook to prevent committing code which does not pass the tests.

By putting test/**/*spec.js into the mocha.opts it runs properly with nodejs, but I got the following error with karma:

PhantomJS 1.9.8 (Windows 7 0.0.0) ERROR
  ReferenceError: Can't find variable: test
  at D:/creation/software developer/projects/javascript-project-template/test/mocha.opts:1

By putting "test/**/*spec.js" into the mocha.opts it runs properly with karma, but I got the following error with nodejs:

D:\creation\software developer\projects\javascript-project-template\node_modules\mocha\lib\utils.js:628
        throw new Error("cannot resolve path (or pattern) '" + path + "'");
        ^

Error: cannot resolve path (or pattern) '"test/**/*spec.js"'
    at Object.lookupFiles (D:\creation\software developer\projects\javascript-project-template\node_modules\mocha\lib\utils.js:628:15)
    at D:\creation\software developer\projects\javascript-project-template\node_modules\mocha\bin\_mocha:326:30
    at Array.forEach (native)
    at Object.<anonymous> (D:\creation\software developer\projects\javascript-project-template\node_modules\mocha\bin\_mocha:325:6)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3

So the mocha.opts is not processed properly by using it with karma-mocha. Is there a way to workaround this issue until it is fixed, e.g. by giving the mocha parameter in the karma config file?

@ghost
Copy link

ghost commented Jun 19, 2016

Hi !

I have the same issue.

I have created a PR with a solution to support the opts file.

Only the --require flag is missing in my solution. It would be nice to do a bundle.

Here is the PR: #99

Let me know what you think about it.

When you want to use your mocha.opts file set the configuration as it follows:

client: { mocha: { opts: './path/to/mocha.opts' } }

Before running the test in the browser, the script creates a JS file with the options from the mocha.opts file called config.js.

The config.js exposes a global object with the options. They are parsed when creating the initial configuration object for Karma.

@ghost ghost mentioned this issue Jun 24, 2016
@maksimr
Copy link
Contributor

maksimr commented Nov 7, 2016

Now plugin support mocha.opts. Look at this PR

@maksimr maksimr closed this as completed Nov 7, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@maksimr and others