Skip to content

Commit 429f2c0

Browse files
authored
Merge pull request #13 from testiumjs/mixin-require
Fixin mixin require logic
2 parents 700d4b2 + b5fc336 commit 429f2c0

File tree

8 files changed

+55
-5
lines changed

8 files changed

+55
-5
lines changed

.testiumrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"launch": true,
33
"app": {
44
"command": "testium-example-app"
5+
},
6+
"mixins": {
7+
"browser": [ "test/mixin.js", "test-mixin-module" ]
58
}
69
}

lib/testium-driver-sync.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,24 @@ var Assertions = require('./assert');
4242

4343
function applyMixin(obj, mixin) {
4444
debug('Applying mixin to %s', obj.constructor.name, mixin);
45-
var mixinFile = path.resolve(process.cwd(), mixin);
46-
_.extend(obj, require(mixinFile));
45+
// this is all to be bug-compatible with our old implementation, which
46+
// would parse a mixin path of "test/blah" the same as "./test/blah"
47+
var cwd = process.cwd();
48+
var paths = [path.resolve(cwd, mixin), path.resolve(cwd, 'node_modules', mixin)];
49+
var mixins;
50+
_.forEach(paths, function eachFile(mixinFile) {
51+
try {
52+
mixins = require(mixinFile); // eslint-disable-line global-require
53+
return false;
54+
} catch (err) {
55+
if (err.code !== 'MODULE_NOT_FOUND') throw err;
56+
return undefined;
57+
}
58+
});
59+
if (!mixins) {
60+
throw new Error('couldn\'t find ' + mixin + ' in ' + paths.join(' or '));
61+
}
62+
_.extend(obj, mixins);
4763
}
4864

4965
function applyMixins(obj, mixins) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"nlm": "^2.0.0",
4343
"node-static": "~0.7.7",
4444
"testium-core": "^1.3.0",
45-
"testium-example-app": "^2.1.0"
45+
"testium-example-app": "^2.1.0",
46+
"test-mixin-module": "file:./test/test-mixin-module"
4647
},
4748
"author": {
4849
"name": "Groupon",

test/integration/element.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('element', () => {
139139
const error = assert.throws(() =>
140140
browser.assert.elementLacksText('.only', 'only'));
141141

142-
const expected = 'Assertion failed: elementLacksText: .only\nnotInclude expected needle not to be found in haystack\n- needle: \"only\"\nhaystack: \"only one here\"';
142+
const expected = 'Assertion failed: elementLacksText: .only\nnotInclude expected needle not to be found in haystack\n- needle: \"only\"\n haystack: \"only one here\"';
143143
assert.equal(expected, stripColors(error.message));
144144
});
145145
});
@@ -165,7 +165,7 @@ describe('element', () => {
165165
const error = assert.throws(() =>
166166
browser.assert.elementLacksValue('#text-input', 'initialvalue'));
167167

168-
const expected = 'Assertion failed: elementLacksValue: #text-input\nnotInclude expected needle not to be found in haystack\n- needle: \"initialvalue\"\nhaystack: \"initialvalue\"';
168+
const expected = 'Assertion failed: elementLacksValue: #text-input\nnotInclude expected needle not to be found in haystack\n- needle: \"initialvalue\"\n haystack: \"initialvalue\"';
169169
assert.equal(expected, stripColors(error.message));
170170
});
171171
});

test/integration/mixin.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getBrowser } from '../mini-testium-mocha';
2+
import assert from 'assertive';
3+
4+
describe('mixin', () => {
5+
let browser;
6+
before(async () => (browser = await getBrowser()));
7+
8+
it('exposes the relative path mixed-in method', () => {
9+
assert.equal(10, browser.mixedInMethod());
10+
});
11+
12+
it('exposes the external module mixed-in method', () => {
13+
assert.equal(42, browser.anotherMethod());
14+
});
15+
});

test/mixin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.mixedInBaseMethod = function mixedInBaseMethod(numbers) {
2+
return this.evaluate(`return ${numbers.join(' + ')}`);
3+
};
4+
5+
exports.mixedInMethod = function mixedInMethod() {
6+
return this.mixedInBaseMethod([1, 2, 3, 4]);
7+
};

test/test-mixin-module/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.anotherMethod = function anotherMethod() {
2+
return 42;
3+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "test-mixin-module",
3+
"version": "0.0.0",
4+
"description": "Mixin Fake Test Module"
5+
}

0 commit comments

Comments
 (0)