Discover how to make your tests lightning-fast and error-proof by faking the parts you don't control!
Why Mocking and stubbing in Deno? - Purpose & Use Cases
Imagine testing a function that calls a real database or an external API every time you run your tests.
You have to wait for the network, and the data might change or be unavailable.
Manually calling real services in tests is slow and unreliable.
Tests can fail for reasons outside your code, making debugging hard.
It's also tricky to test error cases or specific responses.
Mocking and stubbing let you replace real parts with fake ones that behave as you want.
This makes tests fast, stable, and predictable.
You can simulate any scenario without real dependencies.
const data = await fetchRealApi();
assertEquals(data.status, 200);import { stub } from "https://deno.land/std/testing/mock.ts"; const mockApi = stub(api, 'fetch', () => Promise.resolve({ status: 200 })); const data = await api.fetch(); assertEquals(data.status, 200); mockApi.restore();
It enables writing fast, reliable tests that focus only on your code's logic, not external systems.
Testing a payment system without charging real cards or calling the bank's API every time.
Manual testing with real services is slow and flaky.
Mocking and stubbing replace real parts with controllable fakes.
This makes tests fast, stable, and easy to write.
Practice
Solution
Step 1: Understand mocking and stubbing
Mocking and stubbing are techniques used to replace parts of code with fake versions to isolate and test specific behaviors.Step 2: Identify the purpose in testing context
They help test code without relying on real dependencies, making tests simpler and more reliable.Final Answer:
To replace parts of code with fake versions for easier testing -> Option BQuick Check:
Mocking purpose = Replace code parts [OK]
- Thinking mocking speeds up the whole program
- Believing mocks change production code permanently
- Confusing mocking with UI generation
stub function from Deno's standard testing library?Solution
Step 1: Recall the correct import path and syntax
Thestubfunction is exported from "https://deno.land/std/testing/mock.ts" and must be imported using named import syntax.Step 2: Check each option
import { stub } from "https://deno.land/std/testing/mock.ts"; uses named import from the correct path. import stub from "https://deno.land/std/testing/mock.ts"; uses default import which is incorrect. import { stub } from "https://deno.land/std/testing/stub.ts"; uses wrong file name. import * as stub from "https://deno.land/std/testing/mock.ts"; imports all as an object, not the function directly.Final Answer:
import { stub } from "https://deno.land/std/testing/mock.ts"; -> Option CQuick Check:
Correct import = named from mock.ts [OK]
- Using default import instead of named import
- Wrong file path for stub
- Importing everything instead of just stub
import { stub } from "https://deno.land/std/testing/mock.ts";
const obj = {
greet() {
return "Hello";
}
};
const stubGreet = stub(obj, "greet", () => "Hi there!");
console.log(obj.greet());
stubGreet.restore();
console.log(obj.greet());Solution
Step 1: Understand stub behavior on method
The stub replacesobj.greetwith a new function returning "Hi there!" temporarily.Step 2: Analyze console outputs
Firstconsole.logcalls stubbed method, outputting "Hi there!". Afterrestore(), original method returns "Hello" on second log.Final Answer:
"Hi there!" then "Hello" -> Option DQuick Check:
Stub replaces then restores method [OK]
- Expecting original method output before restore
- Thinking stub permanently changes method
- Confusing stub with spy that tracks calls
import { stub } from "https://deno.land/std/testing/mock.ts";
const obj = {
value() {
return 42;
}
};
const stubValue = stub(obj, "val", () => 100);
console.log(obj.value());
stubValue.restore();Solution
Step 1: Check stub target method name
The stub tries to replace method named "val" but obj only has "value" method.Step 2: Understand stub behavior on invalid method
Stubbing a non-existent method causes an error or does not work as expected.Final Answer:
Trying to stub a non-existent method 'val' on obj -> Option AQuick Check:
Stub method must exist on object [OK]
- Using wrong method name in stub
- Forgetting to import stub
- Calling restore too early
fetchData() which normally fetches data from the internet. Using Deno's stub, how can you replace fetchData to return a fixed value { success: true } only during the test, and ensure the original fetchData is restored after the test?Solution
Step 1: Create a stub for fetchData returning fixed value
Usestubon the object containingfetchData, replacing it with a function returning{ success: true }.Step 2: Restore original method after test
Store the stub in a variable and callrestore()after the test to revert changes.Final Answer:
Useconst stubFetch = stub(obj, "fetchData", () => ({ success: true }));then callstubFetch.restore()after test -> Option AQuick Check:
Stub with restore keeps tests clean [OK]
- Not restoring stub after test
- Replacing globally without control
- Returning wrong stub value type
Ready to prove it?
Pass the test and this pattern moves unknown
