0
0
DenoHow-ToBeginner ยท 4 min read

How to Mock in Deno Test: Simple Guide with Examples

In Deno tests, you can mock functions by replacing them with custom implementations inside your test files or by using third-party libraries like mock from std/testing/mock.ts. You create mocks by defining fake functions or objects and injecting them where needed to isolate the code under test.
๐Ÿ“

Syntax

To mock in Deno tests, you typically replace the original function or module with a fake version. You can do this by:

  • Defining a mock function inline.
  • Using the mock utility from Deno's standard library.

Example syntax for a simple mock function:

const mockFunction = () => { /* fake implementation */ };

Or using mock from std/testing/mock.ts:

import { mock } from "https://deno.land/std/testing/mock.ts";

const myMock = mock(() => "mocked result");
typescript
import { mock } from "https://deno.land/std/testing/mock.ts";

const myMock = mock(() => "mocked result");

console.log(myMock()); // prints "mocked result"
Output
mocked result
๐Ÿ’ป

Example

This example shows how to mock a dependency function inside a Deno test by replacing it with a fake version to control its output.

typescript
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

// Original function to be mocked
function fetchData() {
  return "real data";
}

// Function under test uses fetchData
function processData() {
  const data = fetchData();
  return `Processed: ${data}`;
}

Deno.test("processData returns mocked data", () => {
  // Save original function
  const originalFetchData = fetchData;

  // Mock fetchData
  (globalThis as any).fetchData = () => "mocked data";

  // Test with mocked function
  const result = processData();
  assertEquals(result, "Processed: mocked data");

  // Restore original function
  (globalThis as any).fetchData = originalFetchData;
});
Output
test processData returns mocked data ... ok
โš ๏ธ

Common Pitfalls

Common mistakes when mocking in Deno tests include:

  • Not restoring the original function after the test, causing side effects in other tests.
  • Mocking functions globally without proper scope, leading to unpredictable behavior.
  • Trying to mock ES module imports directly, which requires special handling or dependency injection.

Always isolate mocks within tests and restore originals to keep tests independent.

typescript
/* Wrong: Mocking globally without restore */
(globalThis as any).fetchData = () => "bad mock";

/* Right: Save and restore original */
const original = fetchData;
(globalThis as any).fetchData = () => "good mock";
// ...test code...
(globalThis as any).fetchData = original;
๐Ÿ“Š

Quick Reference

ActionHow to Mock in Deno
Mock a function inlineReplace the function with a fake version inside the test scope.
Use std mock utilityImport and use mock from std/testing/mock.ts for simple mocks.
Restore originalAlways save and restore original functions after mocking.
Mock ES modulesUse dependency injection or dynamic imports; direct mocking is complex.
Isolate testsKeep mocks local to tests to avoid side effects.
โœ…

Key Takeaways

Mock functions by replacing them with fake implementations inside your test scope.
Use Deno's standard library mock utility for simple mocking needs.
Always restore original functions after tests to prevent side effects.
Mocking ES module imports requires dependency injection or dynamic import strategies.
Keep mocks isolated within tests for reliable and predictable test results.