Skip to content

Commit

Permalink
Merge pull request #133 from drashland/fix/mock-doesnt-take-args
Browse files Browse the repository at this point in the history
fix: mock object methods cannot take args
  • Loading branch information
crookse authored Dec 1, 2021
2 parents e9c585a + 239c695 commit 41e4603
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 93 deletions.
8 changes: 3 additions & 5 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export { ServerRequest } from "https://deno.land/std@0.106.0/http/server.ts";
export { BufReader } from "https://deno.land/std@0.116.0/io/bufio.ts";

export { BufReader } from "https://deno.land/std@0.106.0/io/bufio.ts";
export * as StdAsserts from "https://deno.land/std@0.116.0/testing/asserts.ts";

export * as StdAsserts from "https://deno.land/[email protected]/testing/asserts.ts";

export * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
export * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
14 changes: 1 addition & 13 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assertions, asserts } from "./src/rhum_asserts.ts";
import { MockServerRequestFn } from "./src/mocks/server_request.ts";
import { TestCase } from "./src/test_case.ts";
import type { ITestPlan, RhumMocks } from "./src/interfaces.ts";
import type { ITestPlan } from "./src/interfaces.ts";
import type { Constructor, Stubbed } from "./src/types.ts";
import { MockBuilder } from "./src/mock_builder.ts";

Expand Down Expand Up @@ -60,8 +59,6 @@ export class RhumRunner {
// deno-lint-ignore ban-types Reason for this is, deno lint no longer allows `Function` and instead needs us to be explicit: `() => void`, but because we couldn't use that to type the properties (we would just be copying Deno's interfaces word for word), we have to deal with `Function
public asserts: { [key in assertions]: Function } = asserts;

public mocks: RhumMocks;

protected passed_in_test_plan = "";

protected passed_in_test_suite = "";
Expand All @@ -72,15 +69,6 @@ export class RhumRunner {

protected plan: ITestPlan = { suites: {} };

// FILE MARKER - METHODS - CONSTRUCTOR ///////////////////////////////////////

/**
* Construct an object of this class.
*/
constructor() {
this.mocks = { ServerRequest: MockServerRequestFn };
}

// FILE MARKER - METHODS - PUBLIC ////////////////////////////////////////////

/**
Expand Down
10 changes: 0 additions & 10 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { MockServerRequestFn } from "./mocks/server_request.ts";

/**
* @remarks
* suites
Expand Down Expand Up @@ -121,11 +119,3 @@ export interface ITestCase {
new_name: string;
testFn: () => void;
}

/**
* ServerRequest
* Type for the ServerRequest on the `Rhum.mocks` property
*/
export interface RhumMocks {
ServerRequest: typeof MockServerRequestFn;
}
4 changes: 2 additions & 2 deletions src/mock_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ export class MockBuilder<T> {
if (!mock.calls[method]) {
mock.calls[method] = 0;
}
mock[method] = function () {
mock[method] = function (...args: unknown[]) {
mock.calls[method]++;
return (original[method as keyof T] as unknown as (
...params: unknown[]
) => unknown)();
) => unknown)(...args);
};
} else {
// copy nativeMethod directly without mocking
Expand Down
59 changes: 0 additions & 59 deletions src/mocks/server_request.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/integration/hooks/after_all_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Rhum.testPlan("after_all_test.ts", () => {
Rhum.afterAll(async () => {
Rhum.asserts.assertEquals(async_case_val, 1);
await new Promise((resolve) => {
setTimeout(() => resolve((async_case_val = 2)), 1000);
setTimeout(() => resolve(async_case_val = 2), 1000);
});
});
Rhum.testCase("Async afterAll hook has no effect before case", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/hooks/after_each_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Rhum.testPlan("after_each_test.ts", () => {
let async_case_val = 5;
Rhum.afterEach(async () => {
await new Promise((resolve) => {
setTimeout(() => resolve((async_case_val = 15)), 1000);
setTimeout(() => resolve(async_case_val = 15), 1000);
});
});
Rhum.testCase("async afterEach hook has no effect before case", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/hooks/before_all_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Rhum.testPlan("before_all_test.ts", () => {
let async_case_val = 5;
Rhum.beforeAll(async () => {
await new Promise((resolve) => {
setTimeout(() => resolve((async_case_val = 15)), 1000);
setTimeout(() => resolve(async_case_val = 15), 1000);
});
});
Rhum.testCase("beforeAll hook can be async", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/hooks/before_each_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Rhum.testPlan("before_each_test.ts", () => {

Rhum.beforeEach(async () => {
await new Promise((resolve) => {
setTimeout(() => resolve((async_case_val = 15)), 1000);
setTimeout(() => resolve(async_case_val = 15), 1000);
});
});

Expand Down
51 changes: 51 additions & 0 deletions tests/integration/mock_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ class TestObject {
}
}

class TestRequestHandler {
async handle(request: Request): Promise<string | Error> {
const method = request.method.toLowerCase();
const contentType = request.headers.get("Content-Type");

if (method !== "post") {
return "Method is not post";
}

if (contentType !== "application/json") {
return "Content-Type is incorrect";
}

return "posted";
}
}

Rhum.testPlan("mock_test.ts", () => {
Rhum.testSuite("mock()", () => {
Rhum.testCase("can mock an object", () => {
Expand Down Expand Up @@ -75,6 +92,40 @@ Rhum.testPlan("mock_test.ts", () => {
mockTestObject.sum(1, 1);
Rhum.asserts.assertEquals(mockMathService.calls.add, 1);
});

Rhum.testCase("Native Request mock", async () => {
const router = Rhum.mock(TestRequestHandler).create();

const reqPost = new Request("https://google.com", {
method: "post",
headers: {
"content-type": "application/json",
},
});
Rhum.asserts.assertEquals(router.calls.handle, 0);
Rhum.asserts.assertEquals(await router.handle(reqPost), "posted");
Rhum.asserts.assertEquals(router.calls.handle, 1);

const reqPostNotJson = new Request("https://google.com", {
method: "post",
});
Rhum.asserts.assertEquals(router.calls.handle, 1);
Rhum.asserts.assertEquals(
await router.handle(reqPostNotJson),
"Content-Type is incorrect",
);
Rhum.asserts.assertEquals(router.calls.handle, 2);

const reqGet = new Request("https://google.com", {
method: "get",
});
Rhum.asserts.assertEquals(router.calls.handle, 2);
Rhum.asserts.assertEquals(
await router.handle(reqGet),
"Method is not post",
);
Rhum.asserts.assertEquals(router.calls.handle, 3);
});
});
});

Expand Down

0 comments on commit 41e4603

Please sign in to comment.