Skip to content

Commit

Permalink
Refactored tests and fixed #6
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Jun 14, 2023
1 parent 20ae127 commit faf68ae
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 61 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"devDependencies": {
"@changesets/cli": "^2.26.0",
"@types/node": "^18.14.5",
"axios": "^1.4.0",
"tsup": "^6.6.3",
"turbo": "^1.8.3",
"typescript": "^4.9.5",
Expand Down
63 changes: 63 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export type NoInfer<T> = [T][T extends any ? 0 : never];
/**
* Adapted from type-fest's PartialDeep
*/
export type PartialDeep<T> = T extends (...args: any[]) => unknown
? T | undefined
export type PartialDeep<T> = T extends (...args: any[]) => any
? Partial<T> | undefined
: T extends object
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
? ItemType[] extends T // Test for arrays (non-tuples) specifically
Expand Down
41 changes: 41 additions & 0 deletions tests/fromAny.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { Equal, Expect, accept } from "./test-utils";
import { fromAny } from "../src";

describe("fromAny", () => {
it("Should return whatever you pass in", () => {
const result = fromAny({ foo: "bar" });

expect(result).toEqual({ foo: "bar" });
});

it("Should return unknown if not called in a function's args", () => {
const result = fromAny({ foo: "bar" });

type test = Expect<Equal<typeof result, unknown>>;
});

it("Should let you pass anything to a function", () => {
accept<{ foo: string; bar: number }>(fromAny("str"));

accept<{ foo: string; bar: number }>(fromAny(124123));

accept<{ foo: string; bar: number }>(
fromAny({
foo: "awdaw",
}),
);
});

it("When passed into a function, it should NOT give excess property warnings", () => {
accept<{
foo: string;
bar: number;
}>(
fromAny({
foo: "bar",
baz: 1,
}),
);
});
});
26 changes: 26 additions & 0 deletions tests/fromExact.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { fromAny, fromExact, fromPartial } from "../src";
import { Equal, Expect, accept } from "./test-utils";

describe("fromExact", () => {
it("Should return whatever you pass in", () => {
const result = fromExact({ foo: "bar" });

expect(result).toEqual({ foo: "bar" });
});

it("Should return the type you pass in", () => {
const result = fromExact({ foo: "bar" });

type test = Expect<Equal<typeof result, { foo: string }>>;
});

it("When passed to a function, it should expect the exact type", () => {
accept<{ foo: string; bar: number }>(
// @ts-expect-error
fromExact({
foo: "bar",
}),
);
});
});
67 changes: 8 additions & 59 deletions tests/utils.test.ts → tests/fromPartial.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { describe, expect, it } from "vitest";
import { fromAny, fromExact, fromPartial } from "../src";
import { Equal, Expect } from "./test-utils";

const accept = <T>(_t: T) => {};
import { fromPartial } from "../src";
import { Equal, Expect, accept } from "./test-utils";
import { AxiosInstance } from "axios";

describe("fromPartial", () => {
it("Should return whatever you pass in", () => {
Expand Down Expand Up @@ -67,65 +66,15 @@ describe("fromPartial", () => {
}),
);
});
});

describe("fromAny", () => {
it("Should return whatever you pass in", () => {
const result = fromAny({ foo: "bar" });

expect(result).toEqual({ foo: "bar" });
});

it("Should return unknown if not called in a function's args", () => {
const result = fromAny({ foo: "bar" });

type test = Expect<Equal<typeof result, unknown>>;
});

it("Should let you pass anything to a function", () => {
accept<{ foo: string; bar: number }>(fromAny("str"));

accept<{ foo: string; bar: number }>(fromAny(124123));

accept<{ foo: string; bar: number }>(
fromAny({
foo: "awdaw",
}),
);
it("Should accept functions", () => {
accept<() => void>(fromPartial({}));
});

it("When passed into a function, it should NOT give excess property warnings", () => {
it("Should accept interfaces which combine object properties and a call signature", () => {
accept<{
(): void;
foo: string;
bar: number;
}>(
fromAny({
foo: "bar",
baz: 1,
}),
);
});
});

describe("fromExact", () => {
it("Should return whatever you pass in", () => {
const result = fromExact({ foo: "bar" });

expect(result).toEqual({ foo: "bar" });
});

it("Should return the type you pass in", () => {
const result = fromExact({ foo: "bar" });

type test = Expect<Equal<typeof result, { foo: string }>>;
});

it("When passed to a function, it should expect the exact type", () => {
accept<{ foo: string; bar: number }>(
// @ts-expect-error
fromExact({
foo: "bar",
}),
);
}>(fromPartial({}));
});
});
2 changes: 2 additions & 0 deletions tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export type UnionToIntersection<U> = (
: never;

export const doNotExecute = (func: () => any) => {};

export const accept = <T>(_t: T) => {};

0 comments on commit faf68ae

Please sign in to comment.