Skip to content

Commit

Permalink
feat: mock no longer returns a disposable
Browse files Browse the repository at this point in the history
This enables you to write `beforeEach(() => mock())`
  • Loading branch information
hasundue committed Jun 22, 2024
1 parent b7d64eb commit 6921d20
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 43 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ cmd.mock();
assert(Deno.Command !== Original);
```

Return a disposable:

```typescript
assert(Symbol.dispose in cmd.mock());
```

#### `use`

Replace Deno.Command inside the callback:
Expand Down Expand Up @@ -143,12 +137,6 @@ import * as fs from "jsr:@chiezo/amber/fs";

#### `mock`

Return a disposable:

```typescript
assert(Symbol.dispose in fs.mock());
```

Replace file system functions as side effects:

```typescript
Expand Down
17 changes: 6 additions & 11 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import type { ConstructorSpy } from "@std/testing/mock";
import * as std from "@std/testing/mock";
import { tryFinally } from "./internal.ts";

export interface Spy<Command extends string | URL>
extends
Disposable,
ConstructorSpy<
Deno.Command,
[command: Command, options?: Deno.CommandOptions]
> {}
export interface Spy<Command extends string | URL> extends
ConstructorSpy<
Deno.Command,
[command: Command, options?: Deno.CommandOptions]
> {}

export interface Stub<Command extends string | URL> extends Spy<Command> {
fake: typeof Deno.Command;
Expand Down Expand Up @@ -95,11 +93,8 @@ export function dispose() {
spies.clear();
}

export function mock(): Disposable {
export function mock() {
Deno.Command = CommandProxy;
return {
[Symbol.dispose]: dispose,
};
}

export function use<T>(fn: () => T): T {
Expand Down
4 changes: 0 additions & 4 deletions src/cmd_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ describe("mock", () => {
cmd.mock();
assert(Deno.Command !== Original);
});

it("should return a disposable", () => {
assert(Symbol.dispose in cmd.mock());
});
});

describe("use", () => {
Expand Down
13 changes: 2 additions & 11 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type FsSpy = {
};

/** A record of spies for Deno APIs related to file system operations. */
export interface FileSystemSpy extends Disposable, FsSpy {
export interface FileSystemSpy extends FsSpy {
}

/** A record of stubs for Deno APIs related to file system operations. */
Expand Down Expand Up @@ -209,10 +209,6 @@ export function stub(
fs,
(fn, name) => fake[name] ? std.spy(fake, name) : fn,
),
[Symbol.dispose]: () => {
spies.delete(path);
fs.removeSync(temp, { recursive: true });
},
} as FileSystemStub;

spies.set(path, stub);
Expand All @@ -225,16 +221,11 @@ export function spy(
return stub(path, createFs());
}

export function mock(): Disposable {
export function mock() {
if (spies.size === 0) {
stub(Deno.cwd());
}
FsFnNames.forEach((name) => mockFsFn(name));
return {
[Symbol.dispose]() {
dispose();
},
};
}

function mockFsFn<T extends FsFnName>(name: T) {
Expand Down
4 changes: 0 additions & 4 deletions src/fs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ describe("mock", () => {

afterEach(() => fs.dispose());

it("should return a disposable", () => {
assert(Symbol.dispose in fs.mock());
});

it("should replace file system functions as side effects", () => {
fs.mock();
assert(Deno.readTextFile !== original.readTextFile);
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { tryFinally } from "./internal.ts";

export interface MockModule {
dispose(): void;
mock(): Disposable;
mock(): void;
restore(): void;
use<T>(fn: () => T): T;
}
Expand Down

0 comments on commit 6921d20

Please sign in to comment.