diff --git a/lib/mocha.js b/lib/mocha.js index 2b2dc13341..63dfdd3c43 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -260,25 +260,30 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) { * @public * @see {@link https://mochajs.org/#-u---ui-name|CLI option} * @see {@link https://mochajs.org/#interfaces|Interface DSLs} - * @param {string} [name=bdd] - Interface name. + * @param {string|Function} [ui=bdd] - Interface name or class. * @returns {Mocha} this * @chainable * @throws {Error} if requested interface cannot be loaded */ -Mocha.prototype.ui = function(name) { - name = name || 'bdd'; - this._ui = exports.interfaces[name]; - if (!this._ui) { - try { - this._ui = require(name); - } catch (err) { - throw createInvalidInterfaceError( - 'invalid interface ' + sQuote(name), - name - ); +Mocha.prototype.ui = function(ui) { + var bindInterface; + if (typeof ui === 'function') { + bindInterface = ui; + } else { + ui = ui || 'bdd'; + bindInterface = exports.interfaces[ui]; + if (!bindInterface) { + try { + bindInterface = require(ui); + } catch (err) { + throw createInvalidInterfaceError( + 'invalid interface ' + sQuote(ui), + ui + ); + } } } - this._ui = this._ui(this.suite); + bindInterface(this.suite); this.suite.on(EVENT_FILE_PRE_REQUIRE, function(context) { exports.afterEach = context.afterEach || context.teardown; diff --git a/test/integration/fixtures/regression/1794/simple-ui.fixture.js b/test/integration/fixtures/simple-ui.fixture.js similarity index 86% rename from test/integration/fixtures/regression/1794/simple-ui.fixture.js rename to test/integration/fixtures/simple-ui.fixture.js index 0e8c666b83..eb4f787f3e 100644 --- a/test/integration/fixtures/regression/1794/simple-ui.fixture.js +++ b/test/integration/fixtures/simple-ui.fixture.js @@ -1,6 +1,6 @@ 'use strict'; -var Mocha = require('../../../../../lib/mocha'); +var Mocha = require('../../../lib/mocha'); var Test = Mocha.Test; var EVENT_FILE_PRE_REQUIRE = Mocha.Suite.constants.EVENT_FILE_PRE_REQUIRE; @@ -13,7 +13,7 @@ module.exports = Mocha.interfaces['simple-ui'] = function(suite) { file, mocha ) { - var common = require('../../../../../lib/interfaces/common')( + var common = require('../../../lib/interfaces/common')( [suite], context ); diff --git a/test/integration/fixtures/regression/1794/issue-1794.fixture.js b/test/integration/fixtures/test-for-simple-ui.fixture.js similarity index 100% rename from test/integration/fixtures/regression/1794/issue-1794.fixture.js rename to test/integration/fixtures/test-for-simple-ui.fixture.js diff --git a/test/integration/options/ui.spec.js b/test/integration/options/ui.spec.js new file mode 100644 index 0000000000..3d0aaf390c --- /dev/null +++ b/test/integration/options/ui.spec.js @@ -0,0 +1,34 @@ +'use strict'; + +var helpers = require('../helpers'); +var runMocha = helpers.runMocha; + +describe('--ui', function() { + var simpleUiPath = require.resolve('../fixtures/simple-ui.fixture'); + + it('should load interface and run it', function(done) { + runMocha('test-for-simple-ui', ['--ui', simpleUiPath], function(err, res) { + if (err) { + done(err); + return; + } + expect(res, 'to have passed'); + done(); + }); + }); + + it("should work if required and name added to Mocha's `interfaces` prop", function(done) { + runMocha( + 'test-for-simple-ui', + ['--require', simpleUiPath, '--ui', 'simple-ui'], + function(err, res) { + if (err) { + done(err); + return; + } + expect(res, 'to have passed'); + done(); + } + ); + }); +}); diff --git a/test/integration/regression.spec.js b/test/integration/regression.spec.js index b122311383..ae4f4fba59 100644 --- a/test/integration/regression.spec.js +++ b/test/integration/regression.spec.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var run = require('./helpers').runMocha; var runJSON = require('./helpers').runMochaJSON; @@ -25,25 +24,6 @@ describe('regressions', function() { }); }); - it("issue-1794: Can't --require custom UI and use it", function(done) { - var simpleUiPath = path.join( - __dirname, - 'fixtures', - 'regression', - '1794', - 'simple-ui.fixture.js' - ); - var args = ['--require', simpleUiPath, '--ui', 'simple-ui']; - run('regression/1794/issue-1794.fixture.js', args, function(err, res) { - if (err) { - done(err); - return; - } - expect(res, 'to have passed'); - done(); - }); - }); - it('issue-1991: Declarations do not get cleaned up unless you set them to `null` - Memory Leak', function(done) { // on a modern MBP takes ±5 seconds on node 4.0, but on older laptops with node 0.12 ±40 seconds. // Could easily take longer on even weaker machines (Travis-CI containers for example).