Skip to content

c4spar/mock-command

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-command

JSR JSR Score

Test utilities to intercept and mock shell commands made with Deno.Command.

Note

The full documentation can be found on jsr.io.

Examples

Mock Deno.Command in a single test

import { assertEquals } from "@std/assert";
import { mockCommand, resetCommand } from "@c4spar/mock-command";

Deno.test({
  name: "should mock a shell command made with Deno.Command",
  async fn() {
    mockCommand({
      command: "deno",
      args: ["run", "example.ts"],
    }, {
      stdout: new TextEncoder().encode("example output"),
    });

    const cmd = new Deno.Command("deno", {
      args: ["run", "example.ts"],
    });
    const output = await cmd.output();

    assertEquals(output, {
      stdout: new TextEncoder().encode("example output"),
      stderr: new Uint8Array(),
      code: 0,
      success: true,
      signal: null,
    });

    resetCommand();
  },
});

Mock Deno.Command for all tests in a test file

import { assertEquals } from "@std/assert";
import {
  mockCommand,
  mockGlobalCommand,
  resetCommand,
  resetGlobalCommand,
} from "@c4spar/mock-command";

Deno.test("MyLib", async (ctx) => {
  mockGlobalCommand();

  await ctx.step({
    name: "should mock a shell command made with Deno.Command",
    async fn() {
      mockCommand({
        command: "deno",
        args: ["run", "example.ts"],
      }, {
        stdout: new TextEncoder().encode("example output"),
      });

      const cmd = new Deno.Command("deno", {
        args: ["run", "example.ts"],
      });
      const output = await cmd.output();

      assertEquals(output, {
        stdout: new TextEncoder().encode("example output"),
        stderr: new Uint8Array(),
        code: 0,
        success: true,
        signal: null,
      });

      resetCommand();
    },
  });

  // More test steps...

  resetGlobalCommand();
});

About

Test utilities to intercept and mock shell commands made with Deno.Command.

Resources

License

Stars

Watchers

Forks

Contributors