Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OriginalObject not assignable to type object #174

Merged
merged 3 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ export function Stub<OriginalObject, ReturnValue>(
dataMember?: keyof OriginalObject,
returnValue?: ReturnValue,
): unknown {
if (obj === null) {
throw new Error(`Cannot create a stub using Stub(null)`);
}

if (obj === undefined) {
return function stubbed() {
return "stubbed";
Expand Down
4 changes: 3 additions & 1 deletion src/fake/fake_mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export function createFake<OriginalConstructor, OriginalObject>(
methodName,
);

if (!((methodName as string) in this.#original)) {
if (
!((methodName as string) in (this.#original as Record<string, unknown>))
) {
const typeSafeMethodName = String(methodName as string);

throw new FakeError(
Expand Down
4 changes: 3 additions & 1 deletion src/mock/mock_mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ export function createMock<OriginalConstructor, OriginalObject>(
methodName,
);

if (!((methodName as string) in this.#original)) {
if (
!((methodName as string) in (this.#original as Record<string, unknown>))
) {
const typeSafeMethodName = String(methodName);
throw new MockError(
`Method "${typeSafeMethodName}" does not exist.`,
Expand Down
14 changes: 14 additions & 0 deletions tests/deno/unit/mod/stub_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ Deno.test("Stub()", async (t) => {
const stub = Stub();
assertEquals(stub(), "stubbed");
});

await t.step("throws error on Stub(null) calls", () => {
try {
// @ts-ignore This test ensures an error is thrown when `null` is being
// provided as the object containing the property or method to stub. It is
// the first check in the `Stub()` call. Even though `Stub(null)` cannot
// happen in TypeScript if type-checking is on, this can still happen in
// JS. This is ignored because it is being tested in TypeScript, but this
// SHOULD only happen in JavaScript.
Stub(null, "prop");
} catch (error) {
assertEquals(error.message, "Cannot create a stub using Stub(null)");
}
});
});