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

Commit

Permalink
Have SpyCall use any instead of unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune committed Nov 14, 2021
1 parent 232da4a commit 8d92103
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
40 changes: 20 additions & 20 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.11.0-success)](https://github.com/udibo/mock/releases/tag/0.11.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/mock@0.11.0/mod.ts)
[![release](https://img.shields.io/badge/release-0.12.0-success)](https://github.com/udibo/mock/releases/tag/0.12.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/mock@0.12.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/master/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, Spy } from "https://deno.land/x/mock@0.11.0/mod.ts";
import { spy, Spy } from "https://deno.land/x/mock@0.12.0/mod.ts";
// Import from GitHub
import { spy, Spy } "https://raw.githubusercontent.com/udibo/mock/0.11.0/mod.ts";
import { spy, Spy } "https://raw.githubusercontent.com/udibo/mock/0.12.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, spy } from "https://deno.land/x/mock@0.11.0/spy.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.12.0/spy.ts";
// Import from GitHub
import {
Spy,
spy,
} from "https://raw.githubusercontent.com/udibo/mock/0.11.0/spy.ts";
} from "https://raw.githubusercontent.com/udibo/mock/0.12.0/spy.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, spy } from "./mock_0.11.0.js";
import { Spy, spy } from "./mock_0.12.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, spy } from "./mock_0.11.0.mjs";
import { Spy, spy } from "./mock_0.12.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, spy } from "./mock_0.11.0.js";
import { Spy, spy } from "./mock_0.12.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, Spy } from "./mock_0.11.0.js";
import { spy, Spy } from "./mock_0.12.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.11.0/mod.ts) for
See [deno docs](https://doc.deno.land/https/deno.land/x/mock@0.12.0/mod.ts) for
more information.

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

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { assertSpyCall } from "https://deno.land/x/mock@0.11.0/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.11.0/spy.ts";
import { assertSpyCall } from "https://deno.land/x/mock@0.12.0/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.12.0/spy.ts";

function add(
a: number,
Expand All @@ -164,7 +164,7 @@ normally, you can wrap it with a spy.

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.11.0/spy.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.12.0/spy.ts";

function filter<T>(values: T[], callback: (value: T) => boolean): any[] {
return values.filter(callback);
Expand Down Expand Up @@ -196,7 +196,7 @@ spy error saying "already spying on function".

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.11.0/spy.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.12.0/spy.ts";

class Database {
private queries: any;
Expand Down Expand Up @@ -285,7 +285,7 @@ calls made to it.

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@0.11.0/stub.ts";
import { Stub, stub } from "https://deno.land/x/mock@0.12.0/stub.ts";

class Cat {
action(name: string): any {
Expand Down Expand Up @@ -320,7 +320,7 @@ will return undefined to each call after the iterator is done.

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@0.11.0/stub.ts";
import { Stub, stub } from "https://deno.land/x/mock@0.12.0/stub.ts";

class Database {
query(query: string, params: any[]): any[][] {
Expand Down Expand Up @@ -394,7 +394,7 @@ instead of the original, you can create a stub with a replacement function.

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@0.11.0/stub.ts";
import { Stub, stub } from "https://deno.land/x/mock@0.12.0/stub.ts";

class Database {
query(query: string, params: any[]): any[][] {
Expand Down Expand Up @@ -470,8 +470,8 @@ controlled through the fake time instance.

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.11.0/spy.ts";
import { FakeTime } from "https://deno.land/x/mock@0.11.0/time.ts";
import { Spy, spy } from "https://deno.land/x/mock@0.12.0/spy.ts";
import { FakeTime } from "https://deno.land/x/mock@0.12.0/time.ts";

function secondInterval(cb: () => void): void {
setInterval(cb, 1000);
Expand Down
6 changes: 4 additions & 2 deletions spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export class SpyError extends Error {
/** Call information recorded by a spy. */
export interface SpyCall {
/** Arguments passed to a function when called. */
args: unknown[];
// deno-lint-ignore no-explicit-any
args: any[];
/** The instance that a method was called on. */
self?: unknown;
// deno-lint-ignore no-explicit-any
self?: any;
/** The value that was returned by a function. */
// deno-lint-ignore no-explicit-any
returned?: any;
Expand Down
3 changes: 2 additions & 1 deletion stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { AnySpyInternal, Spy, spy, SpyCall, SpyError } from "./spy.ts";
/** An instance method wrapper that overrides the original method and records all calls made to it. */
export interface Stub<T> extends Spy<T> {
/** The original value that was replaced with the stub. */
original: unknown;
// deno-lint-ignore no-explicit-any
original: any;
}
export type AnyStub<T> = Stub<T> | Stub<void>;

Expand Down

0 comments on commit 8d92103

Please sign in to comment.