Complete the code to create a mock function using Deno's standard testing library.
import { [1] } from "https://deno.land/std/testing/mock.ts"; const mockFn = [1]();
The mock function from Deno's testing library creates a mock function that can be used to replace real functions during tests.
Complete the code to stub a method on an object using Deno's stub function.
import { stub } from "https://deno.land/std/testing/mock.ts"; const obj = { greet: () => "Hello" }; stub(obj, "greet", [1]);
The stub function replaces the original method with the provided function. Here, we pass a function that returns "Hi".
Fix the error in the code to restore the original method after stubbing.
import { stub } from "https://deno.land/std/testing/mock.ts"; const obj = { greet: () => "Hello" }; const greetStub = stub(obj, "greet", () => "Hi"); // After test [1]();
To restore the original method, you must call the restore() method on the stub instance.
Fill both blanks to create a stub that returns a resolved promise with value 42.
import { stub } from "https://deno.land/std/testing/mock.ts"; const api = { fetchData: async () => 0 }; stub(api, "fetchData", [1]); const result = await api.fetchData(); // result should be 42 // result === [2]
The stub replaces fetchData with an async function returning a resolved promise with 42. The result is the number 42.
Fill all three blanks to create a mock function, call it with argument 5, and check the call count.
import { mock } from "https://deno.land/std/testing/mock.ts"; const fn = [1](); fn([2]); const calls = fn.[3]; // calls should be 1
calls.length instead of callCount.The mock function creates a mock function. Calling it with 5 records the call. The callCount property shows how many times it was called.
