Skip to content

Commit 6921d20

Browse files
committed
feat: mock no longer returns a disposable
This enables you to write `beforeEach(() => mock())`
1 parent b7d64eb commit 6921d20

File tree

6 files changed

+9
-43
lines changed

6 files changed

+9
-43
lines changed

README.md

-12
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ cmd.mock();
2727
assert(Deno.Command !== Original);
2828
```
2929

30-
Return a disposable:
31-
32-
```typescript
33-
assert(Symbol.dispose in cmd.mock());
34-
```
35-
3630
#### `use`
3731

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

144138
#### `mock`
145139

146-
Return a disposable:
147-
148-
```typescript
149-
assert(Symbol.dispose in fs.mock());
150-
```
151-
152140
Replace file system functions as side effects:
153141

154142
```typescript

src/cmd.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import type { ConstructorSpy } from "@std/testing/mock";
22
import * as std from "@std/testing/mock";
33
import { tryFinally } from "./internal.ts";
44

5-
export interface Spy<Command extends string | URL>
6-
extends
7-
Disposable,
8-
ConstructorSpy<
9-
Deno.Command,
10-
[command: Command, options?: Deno.CommandOptions]
11-
> {}
5+
export interface Spy<Command extends string | URL> extends
6+
ConstructorSpy<
7+
Deno.Command,
8+
[command: Command, options?: Deno.CommandOptions]
9+
> {}
1210

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

98-
export function mock(): Disposable {
96+
export function mock() {
9997
Deno.Command = CommandProxy;
100-
return {
101-
[Symbol.dispose]: dispose,
102-
};
10398
}
10499

105100
export function use<T>(fn: () => T): T {

src/cmd_test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ describe("mock", () => {
1414
cmd.mock();
1515
assert(Deno.Command !== Original);
1616
});
17-
18-
it("should return a disposable", () => {
19-
assert(Symbol.dispose in cmd.mock());
20-
});
2117
});
2218

2319
describe("use", () => {

src/fs.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type FsSpy = {
105105
};
106106

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

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

218214
spies.set(path, stub);
@@ -225,16 +221,11 @@ export function spy(
225221
return stub(path, createFs());
226222
}
227223

228-
export function mock(): Disposable {
224+
export function mock() {
229225
if (spies.size === 0) {
230226
stub(Deno.cwd());
231227
}
232228
FsFnNames.forEach((name) => mockFsFn(name));
233-
return {
234-
[Symbol.dispose]() {
235-
dispose();
236-
},
237-
};
238229
}
239230

240231
function mockFsFn<T extends FsFnName>(name: T) {

src/fs_test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ describe("mock", () => {
88

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

11-
it("should return a disposable", () => {
12-
assert(Symbol.dispose in fs.mock());
13-
});
14-
1511
it("should replace file system functions as side effects", () => {
1612
fs.mock();
1713
assert(Deno.readTextFile !== original.readTextFile);

src/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { tryFinally } from "./internal.ts";
22

33
export interface MockModule {
44
dispose(): void;
5-
mock(): Disposable;
5+
mock(): void;
66
restore(): void;
77
use<T>(fn: () => T): T;
88
}

0 commit comments

Comments
 (0)