Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Replace unknown defaults with any defaults and make self assertions u…
Browse files Browse the repository at this point in the history
…se assertEquals (#37)

* Replace unknown defaults with any defaults

* Fix this on default stub

* Make asserts self use assertEquals

* Revert "Fix this on default stub"

This reverts commit 7c8b935.
  • Loading branch information
KyleJune authored Mar 21, 2022
1 parent 7fcf27b commit 38be294
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 50 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mock

[![release](https://img.shields.io/badge/release-0.14.0-success)](https://github.com/udibo/mock/releases/tag/0.14.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/mock@0.14.0/mod.ts)
[![release](https://img.shields.io/badge/release-0.15.0-success)](https://github.com/udibo/mock/releases/tag/0.15.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/mock@0.15.0/mod.ts)
[![CI](https://github.com/udibo/mock/workflows/CI/badge.svg)](https://github.com/udibo/mock/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/udibo/mock/branch/main/graph/badge.svg?token=TXORMSEHM7)](https://codecov.io/gh/udibo/mock)
[![license](https://img.shields.io/github/license/udibo/mock)](https://github.com/udibo/mock/blob/master/LICENSE)
Expand Down Expand Up @@ -30,22 +30,22 @@ imported directly from GitHub using raw content URLs.

```ts
// Import from Deno's third party module registry
import { spy, stub } from "https://deno.land/x/mock@0.14.0/mod.ts";
import { spy, stub } from "https://deno.land/x/mock@0.15.0/mod.ts";
// Import from GitHub
import { spy, stub } "https://raw.githubusercontent.com/udibo/mock/0.14.0/mod.ts";
import { spy, stub } "https://raw.githubusercontent.com/udibo/mock/0.15.0/mod.ts";
```
If you do not need all of the sub-modules, you can choose to just import the
sub-modules you need.
```ts
// Import from Deno's third party module registry
import { spy, stub } from "https://deno.land/x/mock@0.14.0/mock.ts";
import { spy, stub } from "https://deno.land/x/mock@0.15.0/mock.ts";
// Import from GitHub
import {
spy,
stub,
} from "https://raw.githubusercontent.com/udibo/mock/0.14.0/mock.ts";
} from "https://raw.githubusercontent.com/udibo/mock/0.15.0/mock.ts";
```

#### Sub-modules
Expand All @@ -69,15 +69,15 @@ If a Node.js package has the type "module" specified in its package.json file,
the JavaScript bundle can be imported as a `.js` file.

```js
import { spy, stub } from "./mock_0.14.0.js";
import { spy, stub } from "./mock_0.15.0.js";
```

The default type for Node.js packages is "commonjs". To import the bundle into a
commonjs package, the file extension of the JavaScript bundle must be changed
from `.js` to `.mjs`.

```js
import { spy, stub } from "./mock_0.14.0.mjs";
import { spy, stub } from "./mock_0.15.0.mjs";
```

See [Node.js Documentation](https://nodejs.org/api/esm.html) for more
Expand All @@ -96,15 +96,15 @@ modules must have the type attribute set to "module".

```js
// main.js
import { spy, stub } from "./mock_0.14.0.js";
import { spy, stub } from "./mock_0.15.0.js";
```

You can also embed a module script directly into an HTML file by placing the
JavaScript code within the body of the script tag.

```html
<script type="module">
import { spy, stub } from "./mock_0.14.0.js";
import { spy, stub } from "./mock_0.15.0.js";
</script>
```

Expand All @@ -120,7 +120,7 @@ a try block then restore the function in a finally block to ensure the original
instance method is restored before continuing to other tests. The same applies
when using fake time.

See [deno docs](https://doc.deno.land/https/deno.land/x/mock@0.14.0/mod.ts) for
See [deno docs](https://doc.deno.land/https/deno.land/x/mock@0.15.0/mod.ts) for
more information.

### Spy
Expand All @@ -136,7 +136,7 @@ for any calls made to it.

```ts
import { assertEquals } from "https://deno.land/std@0.130.0/testing/asserts.ts";
import { assertSpyCall, spy } from "https://deno.land/x/mock@0.14.0/mod.ts";
import { assertSpyCall, spy } from "https://deno.land/x/mock@0.15.0/mod.ts";

function add(
a: number,
Expand Down Expand Up @@ -167,7 +167,7 @@ import {
assertSpyCall,
assertSpyCalls,
spy,
} from "https://deno.land/x/mock@0.14.0/mod.ts";
} from "https://deno.land/x/mock@0.15.0/mod.ts";

function filter<T>(values: T[], callback: (value: T) => boolean): any[] {
return values.filter(callback);
Expand Down Expand Up @@ -202,7 +202,7 @@ import {
assertSpyCall,
assertSpyCalls,
spy,
} from "https://deno.land/x/mock@0.14.0/mod.ts";
} from "https://deno.land/x/mock@0.15.0/mod.ts";

class Database {
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -293,7 +293,7 @@ import {
assertSpyCall,
assertSpyCalls,
stub,
} from "https://deno.land/x/mock@0.14.0/mod.ts";
} from "https://deno.land/x/mock@0.15.0/mod.ts";

class Cat {
action(name: string): any {
Expand Down Expand Up @@ -345,7 +345,7 @@ import {
assertSpyCalls,
resolvesNext,
stub,
} from "https://deno.land/x/mock@0.14.0/mod.ts";
} from "https://deno.land/x/mock@0.15.0/mod.ts";

class Database {
query(_query: string, _params: unknown[]): Promise<unknown[][]> {
Expand Down Expand Up @@ -427,7 +427,7 @@ import {
FakeTime,
Spy,
spy,
} from "https://deno.land/x/mock@0.14.0/mod.ts";
} from "https://deno.land/x/mock@0.15.0/mod.ts";

function secondInterval(cb: () => void): void {
setInterval(cb, 1000);
Expand Down
12 changes: 7 additions & 5 deletions asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AssertionError,
assertIsError,
assertRejects,
assertStrictEquals,
} from "./deps.ts";
import { Spy, SpyCall } from "./mock.ts";

Expand Down Expand Up @@ -33,9 +32,12 @@ export function assertSpyCalls<

/** Call information recorded by a spy. */
export interface ExpectedSpyCall<
Self = unknown,
Args extends unknown[] = unknown[],
Return = unknown,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
// deno-lint-ignore no-explicit-any
Return = any,
> {
/** Arguments passed to a function when called. */
args?: [...Args, ...unknown[]];
Expand Down Expand Up @@ -86,7 +88,7 @@ export function assertSpyCall<

if ("self" in expected) {
try {
assertStrictEquals(call.self, expected.self);
assertEquals(call.self, expected.self);
} catch (e) {
let message = expected.self
? "spy not called as method on expected self:\n"
Expand Down
4 changes: 2 additions & 2 deletions asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Deno.test("assertSpyCall method", () => {
assertThrows(
() =>
assertSpyCall(spyMethod, 1, {
self: new Point(2, 3),
self: new Point(1, 2),
}),
AssertionError,
"spy not called as method on expected self",
Expand Down Expand Up @@ -591,7 +591,7 @@ Deno.test("assertSpyCallAsync method", async () => {
await assertRejects(
() =>
assertSpyCallAsync(spyMethod, 1, {
self: new Point(2, 3),
self: new Point(1, 2),
}),
AssertionError,
"spy not called as method on expected self",
Expand Down
24 changes: 16 additions & 8 deletions callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import { MockError } from "./mock.ts";

/** Creates a function that returns the instance the method was called on. */
export function returnsThis<
Self = unknown,
Args extends unknown[] = unknown[],
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
>(): (this: Self, ...args: Args) => Self {
return function (this: Self): Self {
return this;
};
}

/** Creates a function that returns one of its arguments. */
export function returnsArg<Arg, Self = unknown>(
// deno-lint-ignore no-explicit-any
export function returnsArg<Arg, Self = any>(
idx: number,
): (this: Self, ...args: Arg[]) => Arg {
return function (...args: Arg[]): Arg {
Expand All @@ -24,7 +27,8 @@ export function returnsArg<Arg, Self = unknown>(
/** Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end. */
export function returnsArgs<
Args extends unknown[],
Self = unknown,
// deno-lint-ignore no-explicit-any
Self = any,
>(
start = 0,
end?: number,
Expand All @@ -37,8 +41,10 @@ export function returnsArgs<
/** Creates a function that returns the iterable values. Any iterable values that are errors will be thrown. */
export function returnsNext<
Return,
Self = unknown,
Args extends unknown[] = unknown[],
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
>(
values: Iterable<Return | Error>,
): (this: Self, ...args: Args) => Return {
Expand All @@ -61,8 +67,10 @@ export function returnsNext<
/** Creates a function that resolves the awaited iterable values. Any awaited iterable values that are errors will be thrown. */
export function resolvesNext<
Return,
Self = unknown,
Args extends unknown[] = unknown[],
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
>(
iterable:
| Iterable<Return | Error | Promise<Return | Error>>
Expand Down
44 changes: 29 additions & 15 deletions mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ export class MockError extends Error {

/** Call information recorded by a spy. */
export interface SpyCall<
Self = unknown,
Args extends unknown[] = unknown[],
Return = unknown,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
// deno-lint-ignore no-explicit-any
Return = any,
> {
/** Arguments passed to a function when called. */
args: Args;
Expand All @@ -26,9 +29,12 @@ export interface SpyCall<

/** A function or instance method wrapper that records all calls made to it. */
export interface Spy<
Self,
Args extends unknown[],
Return,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
// deno-lint-ignore no-explicit-any
Return = any,
> {
(this: Self, ...args: Args): Return;
/** The function that is being spied on. */
Expand All @@ -43,8 +49,10 @@ export interface Spy<

/** Wraps a function with a Spy. */
function functionSpy<
Self = unknown,
Args extends unknown[] = unknown[],
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
Return = undefined,
>(): Spy<Self, Args, Return>;
function functionSpy<
Expand Down Expand Up @@ -275,9 +283,11 @@ function methodSpy<

/** Wraps a function or instance method with a Spy. */
export function spy<
Self = unknown,
Args extends unknown[] = unknown[],
Return = unknown,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
Return = undefined,
>(): Spy<Self, Args, Return>;
export function spy<
Self,
Expand Down Expand Up @@ -309,9 +319,12 @@ export function spy<

/** An instance method replacement that records all calls made to it. */
export interface Stub<
Self,
Args extends unknown[],
Return,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
// deno-lint-ignore no-explicit-any
Return = any,
> extends Spy<Self, Args, Return> {
/** The function that is used instead of the original. */
fake: (this: Self, ...args: Args) => Return;
Expand All @@ -320,7 +333,8 @@ export interface Stub<
/** Replaces an instance method with a Stub. */
export function stub<
Self,
Args extends unknown[] = unknown[],
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
Return = undefined,
>(self: Self, property: keyof Self): Stub<Self, Args, Return>;
export function stub<
Expand Down
4 changes: 1 addition & 3 deletions time_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "./deps.ts";
import { FakeDate, FakeTime, NativeDate } from "./time.ts";
import { MockError, spy, SpyCall, stub } from "./mock.ts";
import { assertSpyCall, assertSpyCalls } from "./asserts.ts";
import { assertSpyCall } from "./asserts.ts";

function fromNow(): () => number {
const start: number = Date.now();
Expand Down Expand Up @@ -87,7 +87,6 @@ Deno.test("FakeDate instance methods passthrough to Date instance methods", () =
args,
returned,
});
assertSpyCalls(func, 1);
}
});
Object.getOwnPropertySymbols(Date.prototype).forEach((method: symbol) => {
Expand All @@ -104,7 +103,6 @@ Deno.test("FakeDate instance methods passthrough to Date instance methods", () =
args,
returned,
});
assertSpyCalls(func, 1);
}
});
});
Expand Down

0 comments on commit 38be294

Please sign in to comment.