Extend test/test.js to support testing generated npm packages.

This commit is contained in:
John-David Dalton
2013-09-29 19:19:02 -07:00
parent 5e29e3f1cb
commit 6d3c492518
2 changed files with 204 additions and 91 deletions

View File

@@ -73,7 +73,7 @@
}()); }());
// used to indicate testing a modularized build // used to indicate testing a modularized build
ui.isModularize = /\b(?:commonjs|(index|main)\.js|lodash-(?:amd|node)|modularize)\b/.test([location.pathname, location.search, ui.buildPath]); ui.isModularize = /\b(?:commonjs|(index|main)\.js|lodash-(?:amd|node|npm)|modularize)\b/.test([location.pathname, location.search, ui.buildPath]);
// initialize controls // initialize controls
addListener(window, 'load', function() { addListener(window, 'load', function() {

View File

@@ -47,7 +47,7 @@
}); });
/** Used to indicate testing a modularized build */ /** Used to indicate testing a modularized build */
var isModularize = ui.isModularize || /\b(?:commonjs|(index|main)\.js|lodash-(?:amd|node)|modularize)\b/.test([ui.buildPath, ui.urlParams.build, basename]); var isModularize = ui.isModularize || /\b(?:commonjs|(index|main)\.js|lodash-(?:amd|node|npm)|modularize)\b/.test([ui.buildPath, ui.urlParams.build, basename]);
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -101,6 +101,9 @@
toString = Object.prototype.toString, toString = Object.prototype.toString,
Worker = document && root.Worker; Worker = document && root.Worker;
/** Detect if testing `npm` modules */
var isNpm = isModularize && /\bnpm\b/.test([ui.buildPath, ui.urlParams.build]);
/** Detects if running in a PhantomJS web page */ /** Detects if running in a PhantomJS web page */
var isPhantomPage = typeof callPhantom == 'function'; var isPhantomPage = typeof callPhantom == 'function';
@@ -489,26 +492,41 @@
(function() { (function() {
test('should return a wrapped value', 1, function() { test('should return a wrapped value', 1, function() {
var actual = _.chain({ 'a': 0 }); if (!isNpm) {
ok(actual instanceof _); var actual = _.chain({ 'a': 0 });
ok(actual instanceof _);
}
else {
skipTest();
}
}); });
test('should return the existing wrapper when chaining', 1, function() { test('should return the existing wrapper when chaining', 1, function() {
var wrapper = _({ 'a': 0 }); if (!isNpm) {
equal(wrapper.chain(), wrapper); var wrapper = _({ 'a': 0 });
equal(wrapper.chain(), wrapper);
}
else {
skipTest();
}
}); });
test('should enable chaining of methods that return unwrapped values by default', 6, function() { test('should enable chaining of methods that return unwrapped values by default', 6, function() {
var array = ['abc']; if (!isNpm) {
var array = ['abc'];
ok(_.chain(array).first() instanceof _); ok(_.chain(array).first() instanceof _);
ok(_(array).chain().first() instanceof _); ok(_(array).chain().first() instanceof _);
ok(_.chain(array).isArray() instanceof _); ok(_.chain(array).isArray() instanceof _);
ok(_(array).chain().isArray() instanceof _); ok(_(array).chain().isArray() instanceof _);
ok(_.chain(array).first().first() instanceof _); ok(_.chain(array).first().first() instanceof _);
ok(_(array).chain().first().first() instanceof _); ok(_(array).chain().first().first() instanceof _);
}
else {
skipTest(6);
}
}); });
}()); }());
@@ -1276,26 +1294,36 @@
}); });
test('should chain when passing `n`, `callback`, or `thisArg`', 3, function() { test('should chain when passing `n`, `callback`, or `thisArg`', 3, function() {
var actual = _(array).first(2); if (!isNpm) {
var actual = _(array).first(2);
ok(actual instanceof _); ok(actual instanceof _);
actual = _(array).first(function(num) { actual = _(array).first(function(num) {
return num < 3; return num < 3;
}); });
ok(actual instanceof _); ok(actual instanceof _);
actual = _(array).first(function(num, index) { actual = _(array).first(function(num, index) {
return this[index] < 3; return this[index] < 3;
}, array); }, array);
ok(actual instanceof _); ok(actual instanceof _);
}
else {
skipTest(3);
}
}); });
test('should not chain when arguments are not provided', 1, function() { test('should not chain when arguments are not provided', 1, function() {
var actual = _(array).first(); if (!isNpm) {
strictEqual(actual, 1); var actual = _(array).first();
strictEqual(actual, 1);
}
else {
skipTest();
}
}); });
test('should work with an object for `callback`', 1, function() { test('should work with an object for `callback`', 1, function() {
@@ -1419,8 +1447,13 @@
}); });
test('`_.' + methodName + '` should return the existing wrapper when chaining', 1, function() { test('`_.' + methodName + '` should return the existing wrapper when chaining', 1, function() {
var wrapper = _([1, 2, 3]); if (!isNpm) {
equal(wrapper[methodName](Boolean), wrapper); var wrapper = _([1, 2, 3]);
equal(wrapper[methodName](Boolean), wrapper);
}
else {
skipTest();
}
}); });
test('`_.' + methodName + '` should support the `thisArg` argument', 2, function() { test('`_.' + methodName + '` should support the `thisArg` argument', 2, function() {
@@ -1576,8 +1609,13 @@
var func = _[methodName]; var func = _[methodName];
test('should return the existing wrapper when chaining', 1, function() { test('should return the existing wrapper when chaining', 1, function() {
var wrapper = _({ 'a': 1 }); if (!isNpm) {
equal(wrapper[methodName]({ 'b': 2 }), wrapper); var wrapper = _({ 'a': 1 });
equal(wrapper[methodName]({ 'b': 2 }), wrapper);
}
else {
skipTest();
}
}); });
@@ -2463,26 +2501,36 @@
}); });
test('should chain when passing `n`, `callback`, or `thisArg`', 3, function() { test('should chain when passing `n`, `callback`, or `thisArg`', 3, function() {
var actual = _(array).last(2); if (!isNpm) {
var actual = _(array).last(2);
ok(actual instanceof _); ok(actual instanceof _);
actual = _(array).last(function(num) { actual = _(array).last(function(num) {
return num > 1; return num > 1;
}); });
ok(actual instanceof _); ok(actual instanceof _);
actual = _(array).last(function(num, index) { actual = _(array).last(function(num, index) {
return this[index] > 1; return this[index] > 1;
}, array); }, array);
ok(actual instanceof _); ok(actual instanceof _);
}
else {
skipTest(3);
}
}); });
test('should not chain when arguments are not provided', 1, function() { test('should not chain when arguments are not provided', 1, function() {
var actual = _(array).last(); if (!isNpm) {
equal(actual, 3); var actual = _(array).last();
equal(actual, 3);
}
else {
skipTest();
}
}); });
test('should work with an object for `callback`', 1, function() { test('should work with an object for `callback`', 1, function() {
@@ -2582,8 +2630,13 @@
_.forEach(['max', 'min'], function(methodName) { _.forEach(['max', 'min'], function(methodName) {
test('`_.' + methodName + '` should resolve the correct value when provided an array containing only one value', 1, function() { test('`_.' + methodName + '` should resolve the correct value when provided an array containing only one value', 1, function() {
var actual = _([40])[methodName]().value(); if (!isNpm) {
strictEqual(actual, 40); var actual = _([40])[methodName]().value();
strictEqual(actual, 40);
}
else {
skipTest();
}
}); });
}); });
@@ -2732,10 +2785,8 @@
this.__wrapped__ = value; this.__wrapped__ = value;
} }
lodash.prototype.value = _.prototype.value;
_.mixin(lodash, { 'a': function(a) { return a[0]; } }); _.mixin(lodash, { 'a': function(a) { return a[0]; } });
strictEqual(lodash(['a']).a().value(), 'a'); strictEqual(lodash(['a']).a().__wrapped__, 'a');
}); });
}()); }());
@@ -3490,13 +3541,23 @@
}); });
test('should chain when passing `n`', 1, function() { test('should chain when passing `n`', 1, function() {
var actual = _(array).sample(2); if (!isNpm) {
ok(actual instanceof _); var actual = _(array).sample(2);
ok(actual instanceof _);
}
else {
skipTest();
}
}); });
test('should not chain when arguments are not provided', 1, function() { test('should not chain when arguments are not provided', 1, function() {
var actual = _(array).sample(); if (!isNpm) {
ok(_.contains(array, actual)); var actual = _(array).sample();
ok(_.contains(array, actual));
}
else {
skipTest();
}
}); });
_.forEach({ _.forEach({
@@ -4441,11 +4502,16 @@
(function() { (function() {
test('should remove the value at index `0` when length is `0` (test in IE 8 compatibility mode)', 2, function() { test('should remove the value at index `0` when length is `0` (test in IE 8 compatibility mode)', 2, function() {
var wrapped = _({ '0': 1, 'length': 1 }); if (!isNpm) {
wrapped.shift(); var wrapped = _({ '0': 1, 'length': 1 });
wrapped.shift();
deepEqual(wrapped.keys().value(), ['length']); deepEqual(wrapped.keys().value(), ['length']);
equal(wrapped.first(), undefined); equal(wrapped.first(), undefined);
}
else {
skipTest(2);
}
}); });
}()); }());
@@ -4455,11 +4521,16 @@
(function() { (function() {
test('should remove the value at index `0` when length is `0` (test in IE < 9, and in compatibility mode for IE9)', 2, function() { test('should remove the value at index `0` when length is `0` (test in IE < 9, and in compatibility mode for IE9)', 2, function() {
var wrapped = _({ '0': 1, 'length': 1 }); if (!isNpm) {
wrapped.splice(0, 1); var wrapped = _({ '0': 1, 'length': 1 });
wrapped.splice(0, 1);
deepEqual(wrapped.keys().value(), ['length']); deepEqual(wrapped.keys().value(), ['length']);
equal(wrapped.first(), undefined); equal(wrapped.first(), undefined);
}
else {
skipTest(2);
}
}); });
}()); }());
@@ -4469,8 +4540,13 @@
(function() { (function() {
test('should return the `toString` result of the wrapped value', 1, function() { test('should return the `toString` result of the wrapped value', 1, function() {
var wrapped = _([1, 2, 3]); if (!isNpm) {
equal(String(wrapped), '1,2,3'); var wrapped = _([1, 2, 3]);
equal(String(wrapped), '1,2,3');
}
else {
skipTest();
}
}); });
}()); }());
@@ -4480,8 +4556,13 @@
(function() { (function() {
test('should return the `valueOf` result of the wrapped value', 1, function() { test('should return the `valueOf` result of the wrapped value', 1, function() {
var wrapped = _(123); if (!isNpm) {
equal(Number(wrapped), 123); var wrapped = _(123);
equal(Number(wrapped), 123);
}
else {
skipTest();
}
}); });
}()); }());
@@ -4500,7 +4581,12 @@
_.forEach(funcs, function(methodName) { _.forEach(funcs, function(methodName) {
test('`_.' + methodName + '` should return a wrapped value', 1, function() { test('`_.' + methodName + '` should return a wrapped value', 1, function() {
ok(wrapped[methodName]() instanceof _); if (!isNpm) {
ok(wrapped[methodName]() instanceof _);
}
else {
skipTest();
}
}); });
}); });
}()); }());
@@ -4548,11 +4634,16 @@
_.forEach(funcs, function(methodName) { _.forEach(funcs, function(methodName) {
test('`_(...).' + methodName + '` should return an unwrapped value', 1, function() { test('`_(...).' + methodName + '` should return an unwrapped value', 1, function() {
var result = methodName == 'reduceRight' if (!isNpm) {
? wrapped[methodName](_.identity) var result = methodName == 'reduceRight'
: wrapped[methodName](); ? wrapped[methodName](_.identity)
: wrapped[methodName]();
equal(result instanceof _, false); equal(result instanceof _, false);
}
else {
skipTest();
}
}); });
}); });
}()); }());
@@ -4573,39 +4664,59 @@
_.forEach(funcs, function(methodName) { _.forEach(funcs, function(methodName) {
test('`_(...).' + methodName + '` called without an `n` argument should return an unwrapped value', 1, function() { test('`_(...).' + methodName + '` called without an `n` argument should return an unwrapped value', 1, function() {
equal(typeof wrapped[methodName](), 'number'); if (!isNpm) {
equal(typeof wrapped[methodName](), 'number');
}
else {
skipTest();
}
}); });
test('`_(...).' + methodName + '` called with an `n` argument should return a wrapped value', 1, function() { test('`_(...).' + methodName + '` called with an `n` argument should return a wrapped value', 1, function() {
ok(wrapped[methodName](1) instanceof _); if (!isNpm) {
ok(wrapped[methodName](1) instanceof _);
}
else {
skipTest();
}
}); });
test('`_.' + methodName + '` should return `undefined` when querying falsey arguments without an `n` argument', 1, function() { test('`_.' + methodName + '` should return `undefined` when querying falsey arguments without an `n` argument', 1, function() {
var actual = [], if (!isNpm) {
expected = _.map(falsey, function() { return undefined; }), var actual = [],
func = _[methodName]; expected = _.map(falsey, function() { return undefined; }),
func = _[methodName];
_.forEach(falsey, function(value, index) { _.forEach(falsey, function(value, index) {
try { try {
actual.push(index ? func(value) : func()); actual.push(index ? func(value) : func());
} catch(e) { } } catch(e) { }
}); });
deepEqual(actual, expected); deepEqual(actual, expected);
}
else {
skipTest();
}
}); });
test('`_.' + methodName + '` should return an empty array when querying falsey arguments with an `n` argument', 1, function() { test('`_.' + methodName + '` should return an empty array when querying falsey arguments with an `n` argument', 1, function() {
var actual = [], if (!isNpm) {
expected = _.map(falsey, function() { return []; }), var actual = [],
func = _[methodName]; expected = _.map(falsey, function() { return []; }),
func = _[methodName];
_.forEach(falsey, function(value, index) { _.forEach(falsey, function(value, index) {
try { try {
actual.push(func(value, 2)); actual.push(func(value, 2));
} catch(e) { } } catch(e) { }
}); });
deepEqual(actual, expected); deepEqual(actual, expected);
}
else {
skipTest();
}
}); });
}); });
}()); }());
@@ -4765,10 +4876,12 @@
ok(pass, '`_.' + methodName + '` accepts falsey arguments'); ok(pass, '`_.' + methodName + '` accepts falsey arguments');
}); });
// add a skipped test for `_.runInContext` which is abscent from modularized builds // skip tests for missing methods of modularized builds
if (!_.runInContext) { _.each(['runInContext', 'tap'], function(methodName) {
skipTest(); if (!_[methodName]) {
} skipTest();
}
});
}); });
test('should reject falsey arguments', 14, function() { test('should reject falsey arguments', 14, function() {