Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 24, 2023
1 parent 82d68c7 commit 59d0f6a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions test/defu.test.ts
Expand Up @@ -9,54 +9,55 @@ describe("defu", () => {
it("should copy only missing properties defaults", () => {
const result = defu({ a: "c" }, { a: "bbb", d: "c" });
expect(result).toEqual({ a: "c", d: "c" });
expectTypeOf(result).toEqualTypeOf<{ a: string; d: string }>();
expectTypeOf(result).toMatchTypeOf<{ a: string; d: string }>();
});

it("should fill in values that are null", () => {
const result1 = defu({ a: null as null }, { a: "c", d: "c" });
expect(result1).toEqual({ a: "c", d: "c" });
expectTypeOf(result1).toEqualTypeOf<{ a: string; d: string }>();
expectTypeOf(result1).toMatchTypeOf<{ a: string; d: string }>();

const result2 = defu({ a: "c" }, { a: null as null, d: "c" });
expect(result2).toEqual({ a: "c", d: "c" });
expectTypeOf(result2).toEqualTypeOf<{ a: string; d: string }>();
expectTypeOf(result2).toMatchTypeOf<{ a: string; d: string }>();
});

it("should copy nested values", () => {
const result = defu({ a: { b: "c" } }, { a: { d: "e" } });
expect(result).toEqual({
a: { b: "c", d: "e" },
});
expectTypeOf(result).toEqualTypeOf<{ a: { b: string; d: string } }>();
expectTypeOf(result).toMatchTypeOf<{ a: { b: string; d: string } }>();
});

it("should concat array values by default", () => {
const result = defu({ array: ["a", "b"] }, { array: ["c", "d"] });
expect(result).toEqual({
array: ["a", "b", "c", "d"],
});
expectTypeOf(result).toEqualTypeOf<{ array: string[] }>();
expectTypeOf(result).toMatchTypeOf<{ array: string[] }>();
});

it("should correctly type differing array values", () => {
const item1 = { name: "Name", age: 21 };
const item2 = { name: "Name", age: "42" };
const result = defu({ items: [item1] }, { items: [item2] });
expect(result).toEqual({ items: [item1, item2] });
expectTypeOf(result).toEqualTypeOf<{
expectTypeOf(result).toMatchTypeOf<{
items: Array<
{ name: string; age: number } | { name: string; age: string }
>;
}>();
});

it("should correctly merge different object types", () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const fn = () => 42;
const re = /test/i;

const result = defu({ a: fn }, { a: re });
expect(result).toEqual({ a: fn });
expectTypeOf(result).toEqualTypeOf<{ a: (() => number) | RegExp }>();
expectTypeOf(result).toMatchTypeOf<{ a: (() => number) | RegExp }>();
});

it("should handle non object first param", () => {
Expand All @@ -78,7 +79,7 @@ describe("defu", () => {
b: 2,
c: 3,
});
expectTypeOf(result).toEqualTypeOf<{
expectTypeOf(result).toMatchTypeOf<{
a: string | number;
b: string | number;
c: number;
Expand Down Expand Up @@ -120,7 +121,7 @@ describe("defu", () => {
}
expectTypeOf(
defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig),
).toEqualTypeOf<ExpectedMergedType>();
).toMatchTypeOf<ExpectedMergedType>();
});

it("should allow partials within merge chain", () => {
Expand All @@ -138,11 +139,11 @@ describe("defu", () => {

expectTypeOf(
defu(options ?? {}, { foo: ["test"] }, { bar: ["test2"] }, {}),
).toEqualTypeOf<ExpectedMergedType>();
).toMatchTypeOf<ExpectedMergedType>();

expectTypeOf(
defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {}),
).toEqualTypeOf<ExpectedMergedType>();
).toMatchTypeOf<ExpectedMergedType>();
});

it("custom merger", () => {
Expand All @@ -156,11 +157,12 @@ describe("defu", () => {
});

it("defuFn()", () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const num = () => 20;
expect(
defuFn(
{
ignore: (val) => val.filter((i) => i !== "dist"),
ignore: (val: any) => val.filter((i: any) => i !== "dist"),
num,
ignored: num,
},
Expand All @@ -177,6 +179,7 @@ describe("defu", () => {
});

it("defuArrayFn()", () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const num = () => 20;
expect(
defuArrayFn(
Expand Down

0 comments on commit 59d0f6a

Please sign in to comment.