Skip to content

Commit 3e9938a

Browse files
committed
feat: cmd.mock stubs any command by default
1 parent 0b84004 commit 3e9938a

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

README.md

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

30+
Stub any command by default:
31+
32+
```typescript
33+
cmd.mock();
34+
await new Deno.Command("echo").output();
35+
assertNotRun("echo");
36+
```
37+
3038
#### `use`
3139

3240
Replace Deno.Command inside the callback:
@@ -69,10 +77,7 @@ Stub a command with the default dummy:
6977
```typescript
7078
const echo = cmd.stub("echo");
7179
await cmd.use(() => new Deno.Command("echo").output());
72-
assertEquals(
73-
Deno.permissions.querySync({ name: "run", command: "echo" }).state,
74-
"prompt",
75-
);
80+
assertNotRun("echo");
7681
assertSpyCalls(echo, 1);
7782
```
7883

src/cmd.ts

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ const CommandProxy: typeof Deno.Command = new Proxy(CommandOriginal, {
7575
const [command, options] = args as ConstructorParameters<
7676
typeof Deno.Command
7777
>;
78+
if (spies.size === 0) {
79+
return new (stub(command))(command, options);
80+
}
7881
const spy = spies.get(command.toString());
7982
if (spy) {
8083
return new spy(command, options);

src/cmd_test.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { afterAll, afterEach, describe, it } from "@std/testing/bdd";
33
import { assertSpyCalls } from "@std/testing/mock";
44
import * as cmd from "./cmd.ts";
55

6+
function assertNotRun(command: string) {
7+
assertEquals(
8+
Deno.permissions.querySync({ name: "run", command }).state,
9+
"prompt",
10+
);
11+
}
12+
613
describe("mock", () => {
714
const Original = Deno.Command;
815

@@ -14,6 +21,12 @@ describe("mock", () => {
1421
cmd.mock();
1522
assert(Deno.Command !== Original);
1623
});
24+
25+
it("should stub any command by default", async () => {
26+
cmd.mock();
27+
await new Deno.Command("echo").output();
28+
assertNotRun("echo");
29+
});
1730
});
1831

1932
describe("use", () => {
@@ -53,10 +66,7 @@ describe("stub", () => {
5366
it("should stub a command with the default dummy", async () => {
5467
const echo = cmd.stub("echo");
5568
await cmd.use(() => new Deno.Command("echo").output());
56-
assertEquals(
57-
Deno.permissions.querySync({ name: "run", command: "echo" }).state,
58-
"prompt",
59-
);
69+
assertNotRun("echo");
6070
assertSpyCalls(echo, 1);
6171
});
6272

0 commit comments

Comments
 (0)