From a79c5c434cdecb36f4763cba30b4f1af2b7c392c Mon Sep 17 00:00:00 2001 From: jdalton Date: Thu, 21 Sep 2023 07:45:49 -0700 Subject: [PATCH] wip: unit test fixes continued --- test/ary.spec.js | 15 ++-------- test/cond.spec.js | 41 ++++++++++++++-------------- test/memoize.spec.js | 4 +-- test/once.spec.js | 20 ++++++-------- test/random.spec.js | 10 +++---- test/remove.spec.js | 2 +- test/shuffle.spec.js | 4 +-- test/slice-and-toArray.spec.js | 2 +- test/slice.spec.js | 14 +++++----- test/some.spec.js | 5 ++-- test/sortBy-methods.spec.js | 6 ++-- test/sortBy.spec.js | 10 +++---- test/startsWith-and-endsWith.spec.js | 4 +-- test/tail.spec.js | 6 ++-- 14 files changed, 66 insertions(+), 77 deletions(-) diff --git a/test/ary.spec.js b/test/ary.spec.js index 4f1964cec..2e4eaaa3b 100644 --- a/test/ary.spec.js +++ b/test/ary.spec.js @@ -1,5 +1,6 @@ -import lodashStable, { ary, curry, rearg } from 'lodash'; -import { slice, _ } from './utils'; +import lodashStable from 'lodash'; +import { slice } from './utils'; +import ary from '../src/ary'; describe('ary', () => { function fn(a, b, c) { @@ -72,14 +73,4 @@ describe('ary', () => { expect(actual).toEqual(['a', 'b', 'c']); }); - - it('should work when combined with other methods that use metadata', () => { - const array = ['a', 'b', 'c']; - let includes = curry(rearg(ary(_.includes, 2), 1, 0), 2); - - expect(includes('b')(array, 2)).toBe(true); - - includes = _(_.includes).ary(2).rearg(1, 0).curry(2).value(); - expect(includes('b')(array, 2)).toBe(true); - }); }); diff --git a/test/cond.spec.js b/test/cond.spec.js index 468aa33d8..6d97da9a4 100644 --- a/test/cond.spec.js +++ b/test/cond.spec.js @@ -1,17 +1,18 @@ import lodashStable from 'lodash'; -import { _, stubA, stubB, stubC, slice, stubFalse, stubTrue } from './utils'; +import cond from '../src/cond'; +import { stubA, stubB, stubC, slice, stubFalse, stubTrue } from './utils'; describe('cond', () => { it('should create a conditional function', () => { - const cond = _.cond([ + const resultFunc = cond([ [lodashStable.matches({ a: 1 }), stubA], [lodashStable.matchesProperty('b', 1), stubB], [lodashStable.property('c'), stubC], ]); - expect(cond({ a: 1, b: 2, c: 3 })).toBe('a'); - expect(cond({ a: 0, b: 1, c: 2 })).toBe('b'); - expect(cond({ a: -1, b: 0, c: 1 })).toBe('c'); + expect(resultFunc({ a: 1, b: 2, c: 3 })).toBe('a'); + expect(resultFunc({ a: 0, b: 1, c: 2 })).toBe('b'); + expect(resultFunc({ a: -1, b: 0, c: 1 })).toBe('c'); }); it('should provide arguments to functions', () => { @@ -19,7 +20,7 @@ describe('cond', () => { let args2; const expected = ['a', 'b', 'c']; - const cond = _.cond([ + const resultFunc = cond([ [ function () { args1 || (args1 = slice.call(arguments)); @@ -31,39 +32,39 @@ describe('cond', () => { ], ]); - cond('a', 'b', 'c'); + resultFunc('a', 'b', 'c'); expect(args1).toEqual(expected); expect(args2).toEqual(expected); }); it('should work with predicate shorthands', () => { - const cond = _.cond([ + const resultFunc = cond([ [{ a: 1 }, stubA], [['b', 1], stubB], ['c', stubC], ]); - expect(cond({ a: 1, b: 2, c: 3 })).toBe('a'); - expect(cond({ a: 0, b: 1, c: 2 })).toBe('b'); - expect(cond({ a: -1, b: 0, c: 1 })).toBe('c'); + expect(resultFunc({ a: 1, b: 2, c: 3 })).toBe('a'); + expect(resultFunc({ a: 0, b: 1, c: 2 })).toBe('b'); + expect(resultFunc({ a: -1, b: 0, c: 1 })).toBe('c'); }); it('should return `undefined` when no condition is met', () => { - const cond = _.cond([[stubFalse, stubA]]); - expect(cond({ a: 1 })).toBe(undefined); + const resultFunc = cond([[stubFalse, stubA]]); + expect(resultFunc({ a: 1 })).toBe(undefined); }); - it('should throw a TypeError if `pairs` is not composed of functions', () => { + it('should throw a TypeError if `pairs` is not resultFunc of functions', () => { lodashStable.each([false, true], (value) => { - assert.throws(() => { - _.cond([[stubTrue, value]])(); - }, TypeError); + expect(() => { + cond([[stubTrue, value]])(); + }).toThrowError(TypeError); }); }); it('should use `this` binding of function for `pairs`', () => { - const cond = _.cond([ + const resultFunc = cond([ [ function (a) { return this[a]; @@ -74,7 +75,7 @@ describe('cond', () => { ], ]); - const object = { cond: cond, a: 1, b: 2 }; - expect(object.cond('a', 'b')).toBe(2); + const object = { resultFunc, a: 1, b: 2 }; + expect(object.resultFunc('a', 'b')).toBe(2); }); }); diff --git a/test/memoize.spec.js b/test/memoize.spec.js index c92715482..93d68ce6d 100644 --- a/test/memoize.spec.js +++ b/test/memoize.spec.js @@ -74,9 +74,9 @@ describe('memoize', () => { }); it('should throw a TypeError if `resolve` is truthy and not a function', () => { - assert.throws(() => { + expect(() => { memoize(noop, true); - }, TypeError); + }).toThrowError(TypeError); }); it('should not error if `resolver` is nullish', () => { diff --git a/test/once.spec.js b/test/once.spec.js index 53c15b66a..254e3c4cb 100644 --- a/test/once.spec.js +++ b/test/once.spec.js @@ -1,35 +1,33 @@ -import { _ } from './utils'; +import once from '../once'; describe('once', () => { it('should invoke `func` once', () => { let count = 0; - const once = _.once(() => ++count); + const resultFunc = once(() => ++count); once(); - expect(once()).toBe(1); + expect(resultFunc()).toBe(1); expect(count).toBe(1); }); it('should ignore recursive calls', () => { let count = 0; - var once = _.once(() => { - once(); + var resultFunc = once(() => { + resultFunc(); return ++count; }); - expect(once()).toBe(1); + expect(resultFunc()).toBe(1); expect(count).toBe(1); }); it('should not throw more than once', () => { - const once = _.once(() => { + const resultFunc = once(() => { throw new Error(); }); - assert.throws(once); - - once(); - expect(true); + expect(resultFunc).toThrow(); + expect(resultFunc).not.toThrow(); }); }); diff --git a/test/random.spec.js b/test/random.spec.js index 471cf6d8a..eeba5d1be 100644 --- a/test/random.spec.js +++ b/test/random.spec.js @@ -8,14 +8,14 @@ describe('random', () => { it('should return `0` or `1` when no arguments are given', () => { const actual = lodashStable.uniq(lodashStable.map(array, () => random())).sort(); - expect(actual, [0).toEqual(1]); + expect(actual).toEqual([0, 1]); }); it('should support a `min` and `max`', () => { const min = 5; const max = 10; - assert.ok( + expect( lodashStable.some(array, () => { const result = random(min, max); return result >= min && result <= max; @@ -27,7 +27,7 @@ describe('random', () => { const min = 0; const max = 5; - assert.ok( + expect( lodashStable.some(array, () => { const result = random(max); return result >= min && result <= max; @@ -49,7 +49,7 @@ describe('random', () => { const min = 2 ** 31; const max = 2 ** 62; - assert.ok( + expect( lodashStable.every(array, () => { const result = random(min, max); return result >= min && result <= max; @@ -62,7 +62,7 @@ describe('random', () => { it('should coerce arguments to finite numbers', () => { const actual = [random(NaN, NaN), random('1', '1'), random(Infinity, Infinity)]; - expect(actual, [0, 1).toEqual(MAX_INTEGER]); + expect(actual).toEqual([0, 1, MAX_INTEGER]); }); it('should support floats', () => { diff --git a/test/remove.spec.js b/test/remove.spec.js index eeb39dd55..1771475ac 100644 --- a/test/remove.spec.js +++ b/test/remove.spec.js @@ -22,7 +22,7 @@ describe('remove', () => { return isEven(index); }); - assert.deepStrictEqual(argsList, [ + expect(argsList).toEqual([ [1, 0, clone], [2, 1, clone], [3, 2, clone], diff --git a/test/shuffle.spec.js b/test/shuffle.spec.js index a32d46264..f0d323463 100644 --- a/test/shuffle.spec.js +++ b/test/shuffle.spec.js @@ -6,7 +6,7 @@ describe('shuffle', () => { const object = { a: 1, b: 2, c: 3 }; it('should return a new array', () => { - assert.notStrictEqual(shuffle(array), array); + expect(shuffle(array)).not.toBe(array); }); it('should contain the same elements after a collection is shuffled', () => { @@ -17,7 +17,7 @@ describe('shuffle', () => { it('should shuffle small collections', () => { const actual = lodashStable.times(1000, () => shuffle([1, 2])); - assert.deepStrictEqual(lodashStable.sortBy(lodashStable.uniqBy(actual, String), '0'), [ + expect(lodashStable.sortBy(lodashStable.uniqBy(actual, String), '0')).toEqual([ [1, 2], [2, 1], ]); diff --git a/test/slice-and-toArray.spec.js b/test/slice-and-toArray.spec.js index b6efce970..ff7303c36 100644 --- a/test/slice-and-toArray.spec.js +++ b/test/slice-and-toArray.spec.js @@ -26,7 +26,7 @@ describe('slice and toArray', () => { it(`\`_.${methodName}\` should return a shallow clone of arrays`, () => { const actual = func(array); expect(actual).toEqual(array); - assert.notStrictEqual(actual, array); + expect(actual).not.toBe(array); }); it(`\`_.${methodName}\` should work with a node list for \`collection\``, () => { diff --git a/test/slice.spec.js b/test/slice.spec.js index 2ac052aeb..de9499578 100644 --- a/test/slice.spec.js +++ b/test/slice.spec.js @@ -1,5 +1,5 @@ import lodashStable from 'lodash'; -import { falsey, LARGE_ARRAY_SIZE } from './utils'; +import { falsey } from './utils'; import slice from '../src/slice'; describe('slice', () => { @@ -8,12 +8,12 @@ describe('slice', () => { it('should use a default `start` of `0` and a default `end` of `length`', () => { const actual = slice(array); expect(actual).toEqual(array); - assert.notStrictEqual(actual, array); + expect(actual).not.toBe(array); }); it('should work with a positive `start`', () => { - expect(slice(array, 1), [2).toEqual(3]); - expect(slice(array, 1, 3), [2).toEqual(3]); + expect(slice(array, 1)).toEqual( [2, 3]); + expect(slice(array, 1, 3)).toEqual([2, 3]); }); it('should work with a `start` >= `length`', () => { @@ -67,7 +67,7 @@ describe('slice', () => { }); it('should work with a negative `end`', () => { - expect(slice(array, 0, -1), [1).toEqual(2]); + expect(slice(array, 0, -1)).toEqual([1, 2]); }); it('should work with a negative `end` <= negative `length`', () => { @@ -81,7 +81,7 @@ describe('slice', () => { const actual = lodashStable.map(positions, (pos) => slice.apply(_, [array].concat(pos))); - expect(actual, [[1], [1], [1], [2, 3], [1]).toEqual([]]); + expect(actual).toEqual([[1], [1], [1], [2, 3], [1], []]); }); it('should work as an iteratee for methods like `_.map`', () => { @@ -89,6 +89,6 @@ describe('slice', () => { const actual = lodashStable.map(array, slice); expect(actual).toEqual(array); - assert.notStrictEqual(actual, array); + expect(actual).not.toBe(array); }); }); diff --git a/test/some.spec.js b/test/some.spec.js index c45fae7d5..f714bdc50 100644 --- a/test/some.spec.js +++ b/test/some.spec.js @@ -23,12 +23,11 @@ describe('some', () => { it('should return `true` as soon as `predicate` returns truthy', () => { let count = 0; - assert.strictEqual( + expect( some([null, true, null], (value) => { count++; return value; - }), - true, + }) ); expect(count).toBe(2); diff --git a/test/sortBy-methods.spec.js b/test/sortBy-methods.spec.js index 6f0e0143d..85369b256 100644 --- a/test/sortBy-methods.spec.js +++ b/test/sortBy-methods.spec.js @@ -45,7 +45,7 @@ describe('sortBy methods', () => { it(`\`_.${methodName}\` should sort multiple properties in ascending order`, () => { const actual = func(objects, ['a', 'b']); - expect(actual, [objects[2], objects[0], objects[3]).toEqual(objects[1]]); + expect(actual).toEqual([objects[2], objects[0], objects[3], objects[1]]); }); it(`\`_.${methodName}\` should support iteratees`, () => { @@ -55,7 +55,7 @@ describe('sortBy methods', () => { return object.b; }, ]); - expect(actual, [objects[2], objects[0], objects[3]).toEqual(objects[1]]); + expect(actual).toEqual([objects[2], objects[0], objects[3], objects[1]]); }); it(`\`_.${methodName}\` should perform a stable sort (test in IE > 8 and V8)`, () => { @@ -70,7 +70,7 @@ describe('sortBy methods', () => { var actual = func(objects.concat(null, undefined), ['a', 'b']); } catch (e) {} - assert.deepStrictEqual(actual, [ + expect(actual).toEqual([ objects[2], objects[0], objects[3], diff --git a/test/sortBy.spec.js b/test/sortBy.spec.js index b9704d392..808f9f7aa 100644 --- a/test/sortBy.spec.js +++ b/test/sortBy.spec.js @@ -15,7 +15,7 @@ describe('sortBy', () => { 'b', ); - expect(actual, [1, 2, 3).toEqual(4]); + expect(actual).toEqual([1, 2, 3, 4]); }); it('should use `_.identity` when `iteratee` is nullish', () => { @@ -32,12 +32,12 @@ describe('sortBy', () => { it('should work with `_.property` shorthands', () => { const actual = lodashStable.map(sortBy(objects.concat(undefined), 'b'), 'b'); - expect(actual, [1, 2, 3, 4).toEqual(undefined]); + expect(actual).toEqual([1, 2, 3, 4, undefined]); }); it('should work with an object for `collection`', () => { const actual = sortBy({ a: 1, b: 2, c: 3 }, Math.sin); - expect(actual, [3, 1).toEqual(2]); + expect(actual).toEqual([3, 1, 2]); }); it('should move `NaN`, nullish, and symbol values to the end', () => { @@ -80,7 +80,7 @@ describe('sortBy', () => { return result; }); - expect(actual, [objects[0], objects[2], objects[1]).toEqual(objects[3]]); + expect(actual).toEqual([objects[0], objects[2], objects[1], objects[3]]); }); it('should work as an iteratee for methods like `_.map`', () => { @@ -91,7 +91,7 @@ describe('sortBy', () => { ], sortBy, ); - assert.deepStrictEqual(actual, [ + expect(actual).toEqual([ [1, 2, 3], [1, 2, 3], ]); diff --git a/test/startsWith-and-endsWith.spec.js b/test/startsWith-and-endsWith.spec.js index 5a9f46e2d..1489a20b6 100644 --- a/test/startsWith-and-endsWith.spec.js +++ b/test/startsWith-and-endsWith.spec.js @@ -24,8 +24,8 @@ describe('startsWith and endsWith', () => { expect(func(string, 'b', Object(position))).toBe(true); expect( - func(string, 'b', { toString: lodashStable.constant(String(position)) }), - ).toBeTruthy(); + func(string, 'b', { toString: lodashStable.constant(String(position)) }) + ).toBe(true); }); it('should return `true` when `target` is an empty string regardless of `position`', () => { diff --git a/test/tail.spec.js b/test/tail.spec.js index 374466c35..b6250949a 100644 --- a/test/tail.spec.js +++ b/test/tail.spec.js @@ -1,5 +1,5 @@ import lodashStable from 'lodash'; -import { falsey, stubArray, LARGE_ARRAY_SIZE } from './utils'; +import { falsey, stubArray } from './utils'; import tail from '../src/tail'; describe('tail', () => { @@ -18,7 +18,7 @@ describe('tail', () => { }); it('should exclude the first element', () => { - expect(tail(array), [2).toEqual(3]); + expect(tail(array)).toEqual([2, 3]); }); it('should return an empty when querying empty arrays', () => { @@ -33,7 +33,7 @@ describe('tail', () => { ]; const actual = lodashStable.map(array, tail); - assert.deepStrictEqual(actual, [ + expect(actual).toEqual([ [2, 3], [5, 6], [8, 9],