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
mockutility 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
| Action | How to Mock in Deno |
|---|---|
| Mock a function inline | Replace the function with a fake version inside the test scope. |
| Use std mock utility | Import and use mock from std/testing/mock.ts for simple mocks. |
| Restore original | Always save and restore original functions after mocking. |
| Mock ES modules | Use dependency injection or dynamic imports; direct mocking is complex. |
| Isolate tests | Keep 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.