diff --git a/README.md b/README.md index 29d0951a..1021c98b 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ Run `mochify --help` to see all available options. - `--plugin` specifies a Browserify plugin to add. Can be specified multiple times. Options can be passed with [subargs][]. - `--extension` search for files with the extension in "require" statements. +- `--no-browser-field` turns off package.json browser field resolution. - `--yields` or `-y` changes the yield interval to allow pending I/O to happen. - `--version` or `-v` shows the Mochify version number. - `--help` or `-h` shows usage and all available options. @@ -197,7 +198,8 @@ mochify('./test/*.js', { - `mochify(paths, opts)` combines custom paths and options All long form command line options can be used. E.g. `--node` can be configured -as `{ node : true }`, `--reporter tab` as `{ reporter : 'tab' }` and so on. +as `{ node : true }`, `--no-browser-field` as `{ 'browser-field': false }`, +`--reporter tab` as `{ reporter : 'tab' }` and so on. Additional API options: diff --git a/lib/args.js b/lib/args.js index d68c541a..f50181d5 100644 --- a/lib/args.js +++ b/lib/args.js @@ -17,6 +17,7 @@ var defaults = { wd : false, recursive : false, 'ignore-ssl-errors': false, + 'browser-field': true, reporter : 'dot', timeout : '2000', port : '0', @@ -32,7 +33,7 @@ function args(argv) { 'wd-file', 'path'], boolean : ['help', 'version', 'watch', 'cover', 'node', 'wd', 'debug', 'invert', 'recursive', 'colors', - 'ignore-ssl-errors'], + 'ignore-ssl-errors', 'browser-field'], alias : { help : 'h', version : 'v', diff --git a/lib/help.txt b/lib/help.txt index cf6001a1..d523c8e8 100644 --- a/lib/help.txt +++ b/lib/help.txt @@ -7,7 +7,7 @@ Defaults "entry" to "./test/*.js". -w, --watch Use watchify to watch your files and run the tests on change. - -R, --reporter Change the Mocha reporter. + -R, --reporter Change the Mocha reporter. Mocha reporters known to work: doc, dot (default), json, landing, list, markdown, min, spec, tap, xunit @@ -66,6 +66,9 @@ Defaults "entry" to "./test/*.js". statements. For example, "--extension .coffee". Can be specified multiple times. +--no-browser-field Turn off package.json browser field resolution. This is + also handy if you need to run a bundle in node. + -y, --yields Changes the yield interval to allow pending I/O to happen. -v, --version Print mochify version and exit. diff --git a/lib/mochify.js b/lib/mochify.js index 09ae7645..c3ceed29 100644 --- a/lib/mochify.js +++ b/lib/mochify.js @@ -102,6 +102,7 @@ module.exports = function (_, opts) { brOpts.insertGlobalVars = ['__dirname', '__filename']; } brOpts.extensions = opts.extension; + brOpts.browserField = opts['browser-field']; brOpts.paths = opts.path; var b = browserify(brOpts); diff --git a/test/args-test.js b/test/args-test.js index fc512ed1..c79dc6cd 100644 --- a/test/args-test.js +++ b/test/args-test.js @@ -28,6 +28,7 @@ describe('args', function () { assert.equal(opts.port, 0); assert.equal(opts.yields, 0); assert.equal(opts['ignore-ssl-errors'], false); + assert.equal(opts['browser-field'], true); }); it('parses --reporter', function () { @@ -215,6 +216,18 @@ describe('args', function () { assert.equal(opts.colors, false); }); + it('parses --browser-field', function () { + var opts = args(['--browser-field']); + + assert(opts['browser-field']); + }); + + it('parses --no-browser-field', function () { + var opts = args(['--no-browser-field']); + + assert.equal(opts['browser-field'], false); + }); + it('parses --path', function () { var opts = args(['--path', './source/']); diff --git a/test/fixture/browser-field/node_modules/some-module/browser.js b/test/fixture/browser-field/node_modules/some-module/browser.js new file mode 100644 index 00000000..743eff45 --- /dev/null +++ b/test/fixture/browser-field/node_modules/some-module/browser.js @@ -0,0 +1,2 @@ +'use strict'; +module.exports = 'browser'; diff --git a/test/fixture/browser-field/node_modules/some-module/index.js b/test/fixture/browser-field/node_modules/some-module/index.js new file mode 100644 index 00000000..a847de7e --- /dev/null +++ b/test/fixture/browser-field/node_modules/some-module/index.js @@ -0,0 +1,2 @@ +'use strict'; +module.exports = 'index'; diff --git a/test/fixture/browser-field/node_modules/some-module/package.json b/test/fixture/browser-field/node_modules/some-module/package.json new file mode 100644 index 00000000..2058e638 --- /dev/null +++ b/test/fixture/browser-field/node_modules/some-module/package.json @@ -0,0 +1,6 @@ +{ + "name": "some-module", + "version": "1.0.0", + "main": "index.js", + "browser": "browser.js" +} diff --git a/test/fixture/browser-field/test/browser-field.js b/test/fixture/browser-field/test/browser-field.js new file mode 100644 index 00000000..ea1f15cc --- /dev/null +++ b/test/fixture/browser-field/test/browser-field.js @@ -0,0 +1,14 @@ +/*global describe, it*/ +'use strict'; + +var someModule = require('some-module'); + +describe('browser-field', function () { + + it('passes in browser', function () { + if (someModule !== 'browser') { + throw new Error(); + } + }); + +}); diff --git a/test/node-test.js b/test/node-test.js index 9e20ef91..1d1f5915 100644 --- a/test/node-test.js +++ b/test/node-test.js @@ -257,6 +257,32 @@ describe('node', function () { }); }); + it('passes browser-field', function (done) { + run('browser-field', ['--node', '-R', 'tap'], + function (code, stdout) { + assert.equal(stdout, '# node:\n' + + '1..1\n' + + 'ok 1 browser-field passes in browser\n' + + '# tests 1\n' + + '# pass 1\n' + + '# fail 0\n'); + assert.equal(code, 0); + done(); + }); + }); + + it('fails browser-field with --browser-field disabled', function (done) { + run('browser-field', ['--node', '-R', 'tap', '--no-browser-field'], + function (code, stdout) { + assert.equal(stdout.indexOf('# node:\n' + + '1..1\n' + + 'not ok 1 browser-field passes in browser\n' + + ' Error'), 0); + assert.equal(code, 1); + done(); + }); + }); + // This test case fails on node 0.10 only. The corresponding phantomjs test // passes on node 0.10 and 0.12. it.skip('shows unicode diff', function (done) {