Skip to content

Commit

Permalink
test: convert test to typescript and fixup errors this revealed
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Stevens authored and RebeccaStevens committed Nov 27, 2020
1 parent 13d6db1 commit ded6c51
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 130 deletions.
98 changes: 91 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"presize": "npm run build:js",
"size": "terser --compress --mangle -- ./dist/index.umd.js | gzip -c | wc -c",
"test": "npm run test:js && npm run test:types && npm run test:jsmd",
"pretest:js": "npm run build:js",
"test:js": "tape test/*.js",
"test:js": "ts-node -C typescript -P tsconfig.test.json -r tsconfig-paths/register node_modules/tape/bin/tape test/*.ts",
"pretest:jsmd": "npm run build:js",
"test:jsmd": "jsmd readme.md",
"pretest:types": "npm run build:types",
"test:types": "dtslint --localTs node_modules/typescript/lib --expectOnly test/types"
Expand All @@ -72,17 +72,19 @@
"@rollup/plugin-node-resolve": "^10.0.0",
"@rollup/plugin-typescript": "^6.1.0",
"@types/node": "^8.10.54",
"@types/tape": "^4.13.0",
"@typescript-eslint/eslint-plugin": "^4.8.0",
"@typescript-eslint/parser": "^4.8.0",
"dtslint": "^4.0.5",
"eslint": "^7.13.0",
"is-mergeable-object": "1.1.0",
"is-plain-obj": "^3.0.0",
"jsmd": "^1.0.2",
"rimraf": "^3.0.2",
"rollup": "^2.33.1",
"tape": "^5.0.1",
"terser": "^5.3.8",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"tslib": "^2.0.3",
"typescript": "^4.1.2"
},
Expand Down
8 changes: 4 additions & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isPlainObj from "is-plain-obj"

import { cloneUnlessOtherwiseSpecified } from "./impl"
import type { Property } from "./types"
import type { FlattenAlias, Property } from "./types"

/**
* Deep merge options.
Expand All @@ -23,7 +23,7 @@ export type ExplicitOptions<O extends Options = Options> = {
/**
* Deep merge options with defaults applied.
*/
export type FullOptions<O extends Options = Options> = {
export type FullOptions<O extends Options = Options> = FlattenAlias<{
arrayMerge: O[`arrayMerge`] extends undefined
? typeof defaultArrayMerge
: NonNullable<O[`arrayMerge`]>
Expand All @@ -33,7 +33,7 @@ export type FullOptions<O extends Options = Options> = {
? typeof defaultIsMergeable
: NonNullable<O[`isMergeable`]>
cloneUnlessOtherwiseSpecified: <T>(value: T, options: FullOptions) => T
}
}>

/**
* A function that determins if a type is mergable.
Expand Down Expand Up @@ -85,5 +85,5 @@ export function getFullOptions<O extends Options>(options?: O): FullOptions<O> {
clone: true,
...overrides,
cloneUnlessOtherwiseSpecified,
} as FullOptions<O>
} as unknown as FullOptions<O>
}
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type DeepMergeValues<T1, T2, O extends Options> = And<IsArray<T1>, IsArray<T2>>
/**
* Deep merge 2 non-array objects.
*/
export type DeepMergeObjects<T1, T2, O extends Options> = FlatternAlias<
export type DeepMergeObjects<T1, T2, O extends Options> = FlattenAlias<
// @see https://github.com/microsoft/TypeScript/issues/41448
{
-readonly [K in keyof T1]: DeepMergeObjectProps<ValueOfKey<T1, K>, ValueOfKey<T2, K>, O>
Expand Down Expand Up @@ -118,7 +118,7 @@ type MaybeLeaf<T1, T2> = Or<
* single object.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
type FlatternAlias<T> = {} & { [P in keyof T]: T[P] }
export type FlattenAlias<T> = {} & { [P in keyof T]: T[P] }

/**
* Get the value of the given key in the given object.
Expand Down
23 changes: 11 additions & 12 deletions test/custom-array-merge.js → test/custom-array-merge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const merge = require(`../`).default
const test = require(`tape`)
import type { Options } from "deepmerge"
import deepmerge from "deepmerge"
import test from "tape"

test(`custom merge array`, (t) => {
let mergeFunctionCalled = false
function overwriteMerge(target, source, options) {
const overwriteMerge: Options[`arrayMerge`] = (target, source, options) => {
mergeFunctionCalled = true
t.equal(options.arrayMerge, overwriteMerge)

Expand All @@ -17,7 +18,7 @@ test(`custom merge array`, (t) => {
someArray: [ 1, 2, 3 ],
}

const actual = merge(destination, source, { arrayMerge: overwriteMerge })
const actual = deepmerge(destination, source, { arrayMerge: overwriteMerge })
const expected = {
someArray: [ 1, 2, 3 ],
someObject: { what: `yes` },
Expand All @@ -29,10 +30,8 @@ test(`custom merge array`, (t) => {
})

test(`merge top-level arrays`, (t) => {
function overwriteMerge(a, b) {
return b
}
const actual = merge([ 1, 2 ], [ 1, 2 ], { arrayMerge: overwriteMerge })
const overwriteMerge: Options[`arrayMerge`] = (a, b) => b
const actual = deepmerge([ 1, 2 ], [ 1, 2 ], { arrayMerge: overwriteMerge })
const expected = [ 1, 2 ]

t.deepEqual(actual, expected)
Expand All @@ -41,7 +40,7 @@ test(`merge top-level arrays`, (t) => {

test(`cloner function is available for merge functions to use`, (t) => {
let customMergeWasCalled = false
function cloneMerge(target, source, options) {
const cloneMerge: Options[`arrayMerge`] = (target, source, options) => {
customMergeWasCalled = true
t.ok(options.cloneUnlessOtherwiseSpecified, `cloner function is available`)
return target.concat(source).map((element) => options.cloneUnlessOtherwiseSpecified(element, options))
Expand All @@ -60,9 +59,9 @@ test(`cloner function is available for merge functions to use`, (t) => {
key2: [ `four` ],
}

t.deepEqual(merge(target, src, { arrayMerge: cloneMerge }), expected)
t.deepEqual(deepmerge(target, src, { arrayMerge: cloneMerge }), expected)
t.ok(customMergeWasCalled)
t.ok(Array.isArray(merge(target, src).key1))
t.ok(Array.isArray(merge(target, src).key2))
t.ok(Array.isArray(deepmerge(target, src).key1))
t.ok(Array.isArray(deepmerge(target, src).key2))
t.end()
})
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const merge = require(`../`).default
const test = require(`tape`)
import type { Options } from "deepmerge"
import deepmerge from "deepmerge"
import test from "tape"

test(`isMergeable function copying object over object`, (t) => {
const src = { key: { isMergeable: false }, baz: `yes` }
const target = { key: { foo: `wat` }, baz: `whatever` }

function customIsMergeable(object) {
return object && typeof value === `object` && object.isMergeable !== false
}
const customIsMergeable: Options[`isMergeable`] = (object) =>
object && typeof object === `object` && object.isMergeable !== false

const res = merge(target, src, {
const res = deepmerge(target, src, {
isMergeable: customIsMergeable,
})

Expand All @@ -22,11 +22,10 @@ test(`isMergeable function copying object over nothing`, (t) => {
const src = { key: { isMergeable: false, foo: `bar` }, baz: `yes` }
const target = { baz: `whatever` }

function customIsMergeable(object) {
return object && typeof value === `object` && object.isMergeable !== false
}
const customIsMergeable: Options[`isMergeable`] = (object) =>
object && typeof object === `object` && object.isMergeable !== false

const res = merge(target, src, {
const res = deepmerge(target, src, {
isMergeable: customIsMergeable,
})

Expand Down
Loading

0 comments on commit ded6c51

Please sign in to comment.