From 97ca84f4fd66448bb29ef1f0c11ff864c0099457 Mon Sep 17 00:00:00 2001 From: David Bushong Date: Fri, 21 Jul 2017 18:39:58 -0700 Subject: [PATCH] fix: support `mixins.wd[] = 'some-module'` Per the docs: > Each setting is an array of module names that will be resolved > relative to the working directory. E.g. `./test/mixins/session.js` will > be resolved to `${cwd}/test/mixins/session.js` and `mixins/session` may be > resolved to `${cwd}/node_modules/mixins/session.js`. That wasn't true before this, but since some people may depend on the old behavior to put something like `test/some-file`, we first check for working local path, then try prepending node_modules. --- .testiumrc | 2 +- lib/testium-driver-wd.js | 20 ++++++++++++++++++-- package.json | 1 + test/integration/mixin.test.js | 6 +++++- test/test-mixin-module/index.js | 3 +++ test/test-mixin-module/package.json | 5 +++++ 6 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/test-mixin-module/index.js create mode 100644 test/test-mixin-module/package.json diff --git a/.testiumrc b/.testiumrc index 0b92edf..362273f 100644 --- a/.testiumrc +++ b/.testiumrc @@ -4,6 +4,6 @@ "command": "testium-example-app" }, "mixins": { - "wd": [ "test/mixin.js" ] + "wd": [ "test/mixin.js", "test-mixin-module" ] } } diff --git a/lib/testium-driver-wd.js b/lib/testium-driver-wd.js index ce92334..14fc85e 100644 --- a/lib/testium-driver-wd.js +++ b/lib/testium-driver-wd.js @@ -62,8 +62,24 @@ function applyMethods(methods) { function applyMixin(mixin) { debug('Applying mixin to wd', mixin); - var mixinFile = path.resolve(process.cwd(), mixin); - applyMethods(require(mixinFile)); // eslint-disable-line global-require + // this is all to be bug-compatible with our old implementation, which + // would parse a mixin path of "test/blah" the same as "./test/blah" + var cwd = process.cwd(); + var paths = [path.resolve(cwd, mixin), path.resolve(cwd, 'node_modules', mixin)]; + var mixins; + _.forEach(paths, function eachFile(mixinFile) { + try { + mixins = require(mixinFile); // eslint-disable-line global-require + return false; + } catch (err) { + if (err.code !== 'MODULE_NOT_FOUND') throw err; + return undefined; + } + }); + if (!mixins) { + throw new Error('couldn\'t find ' + mixin + ' in ' + paths.join(' or ')); + } + applyMethods(mixins); } function initDriver(testium) { diff --git a/package.json b/package.json index c1b8927..777911a 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "eslint-config-groupon": "^3.2.0", "eslint-config-groupon-es5": "^3.0.0", "eslint-plugin-import": "^1.6.1", + "test-mixin-module": "file:./test/test-mixin-module", "mocha": "^3.1.2", "nlm": "^3.0.0", "testium-core": "^1.3.0", diff --git a/test/integration/mixin.test.js b/test/integration/mixin.test.js index 60c4f3a..f44235d 100644 --- a/test/integration/mixin.test.js +++ b/test/integration/mixin.test.js @@ -4,7 +4,11 @@ import assert from 'assertive'; describe('mixin', () => { before(browser.beforeHook); - it('exposes the mixed-in method', async () => { + it('exposes the relative path mixed-in method', async () => { assert.equal(10, await browser.mixedInMethod()); }); + + it('exposes the external module mixed-in method', async () => { + assert.equal(42, await browser.anotherMethod()); + }); }); diff --git a/test/test-mixin-module/index.js b/test/test-mixin-module/index.js new file mode 100644 index 0000000..d811e33 --- /dev/null +++ b/test/test-mixin-module/index.js @@ -0,0 +1,3 @@ +exports.anotherMethod = function anotherMethod() { + return 42; +}; diff --git a/test/test-mixin-module/package.json b/test/test-mixin-module/package.json new file mode 100644 index 0000000..d81534e --- /dev/null +++ b/test/test-mixin-module/package.json @@ -0,0 +1,5 @@ +{ + "name": "test-mixin-module", + "version": "0.0.0", + "description": "Mixin Fake Test Module" +}