admin管理员组

文章数量:1416051

I am using Mocha programatically as described here:

Very similarly to how the examples are written:

test-runner.js:

var Mocha = require('mocha');
var mocha = new Mocha();
mocha.addFile('spec.js');
mocha.run(function() {});

Inside the test spec, I am spinning up a headless browser to run the test on a specific url:

spec.js:

var Browser = new Browser();
browser.visit(url, function(){});

Is there a way to pass the desired url from test-runner.js to spec.js?

I am using Mocha programatically as described here:

https://github./visionmedia/mocha/wiki/Using-mocha-programmatically

Very similarly to how the examples are written:

test-runner.js:

var Mocha = require('mocha');
var mocha = new Mocha();
mocha.addFile('spec.js');
mocha.run(function() {});

Inside the test spec, I am spinning up a headless browser to run the test on a specific url:

spec.js:

var Browser = new Browser();
browser.visit(url, function(){});

Is there a way to pass the desired url from test-runner.js to spec.js?

Share Improve this question asked Dec 21, 2012 at 23:36 superdikerysuperdikery 781 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

In your spec file, you can require a module that contains the configurations for those tests:

var url = require('./config.js').url;
describe("blah", function(){
    ...
});

This config module could also be set from the initial mocha tests (e.g.):

var Mocha = require('mocha');
var mocha = new Mocha();

var config = require('./config.js');
config.setOptions({url:"localhost/testme.html"});

mocha.addFile('spec.js');
mocha.run(function() {});

Check out this related SO regarding node modules being singletons.

本文标签: javascriptPassing parameters to MochaStack Overflow