|
| 1 | +--- |
| 2 | +title: "Testing Helpers: Fetch Mocker" |
| 3 | +description: The fetch mocker methods that are helpful for testing. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Aside } from "@astrojs/starlight/components"; |
| 7 | + |
| 8 | +The fetch mocker provides several methods to help verify that your requests were made as expected. |
| 9 | + |
| 10 | +## Verifying Route Calls |
| 11 | + |
| 12 | +Tracking which routes have been called is a key part of many tests. The fetch mocker provides several methods and properties to help with this. |
| 13 | + |
| 14 | +### `allRoutesCalled()` |
| 15 | + |
| 16 | +The `allRoutesCalled()` method checks if all routes have been called on all servers. It returns `true` if all routes have been called at least once, and `false` otherwise: |
| 17 | + |
| 18 | +```js {10, 13, 16} |
| 19 | +import { MockServer, FetchMocker } from "mentoss"; |
| 20 | + |
| 21 | +const server1 = new MockServer("https://api1.example.com"); |
| 22 | +const server2 = new MockServer("https://api2.example.com"); |
| 23 | +const mocker = new FetchMocker({ servers: [server1, server2] }); |
| 24 | + |
| 25 | +server1.get("/users", 200); |
| 26 | +server2.post("/users", 200); |
| 27 | + |
| 28 | +console.log(mocker.allRoutesCalled()); // false |
| 29 | + |
| 30 | +await mocker.fetch("https://api1.example.com/users"); |
| 31 | +console.log(mocker.allRoutesCalled()); // false |
| 32 | + |
| 33 | +await mocker.fetch("https://api2.example.com/users", { method: "POST" }); |
| 34 | +console.log(mocker.allRoutesCalled()); // true |
| 35 | +``` |
| 36 | + |
| 37 | +In this example, the `allRoutesCalled()` method first returns `false` because neither the GET nor the POST `/users` route has been called yet. After both routes have been called, the method returns `true`. |
| 38 | + |
| 39 | +### `assertAllRoutesCalled()` |
| 40 | + |
| 41 | +The `assertAllRoutesCalled()` method is an assertion that checks if all routes have been called. It throws an error if any routes have not been called: |
| 42 | + |
| 43 | +```js {11} |
| 44 | +import { MockServer, FetchMocker } from "mentoss"; |
| 45 | + |
| 46 | +const server1 = new MockServer("https://api1.example.com"); |
| 47 | +const server2 = new MockServer("https://api2.example.com"); |
| 48 | +const mocker = new FetchMocker({ servers: [server1, server2] }); |
| 49 | + |
| 50 | +server1.get("/users", 200); |
| 51 | +server2.post("/users", 200); |
| 52 | + |
| 53 | +await mocker.fetch("https://api1.example.com/users"); |
| 54 | +server.assertAllRoutesCalled(); // Error! |
| 55 | +``` |
| 56 | + |
| 57 | +The thrown error contains a message with information about the uncalled routes. |
| 58 | + |
| 59 | +### `called()` |
| 60 | + |
| 61 | +The `called()` method checks if a specific route has been called. You can pass either a URL string (which defaults to a GET request) or a request pattern object: |
| 62 | + |
| 63 | +```js {17,26, 28-34} |
| 64 | +import { MockServer, FetchMocker } from "mentoss"; |
| 65 | + |
| 66 | +const server1 = new MockServer("https://api1.example.com"); |
| 67 | +const server2 = new MockServer("https://api2.example.com"); |
| 68 | +const mocker = new FetchMocker({ servers: [server1, server2] }); |
| 69 | + |
| 70 | +server1.post( |
| 71 | + { |
| 72 | + url: "/users", |
| 73 | + body: { |
| 74 | + name: "John Doe", |
| 75 | + }, |
| 76 | + }, |
| 77 | + 200, |
| 78 | +); |
| 79 | + |
| 80 | +console.log(mocker.called("https://api1.example.com/users")); // false |
| 81 | + |
| 82 | +await mocker.fetch("https://api1.example.com/users", { |
| 83 | + method: "POST", |
| 84 | + body: { |
| 85 | + name: "John Doe", |
| 86 | + }, |
| 87 | +}); |
| 88 | + |
| 89 | +console.log(mocker.called("https://api1.example.com/users")); // true |
| 90 | + |
| 91 | +console.log(mocker.called({ |
| 92 | + url: "https://api1.example.com/users", |
| 93 | + method: "POST", |
| 94 | + body: { |
| 95 | + name: "John Doe", |
| 96 | + }, |
| 97 | +})); // true |
| 98 | +``` |
| 99 | + |
| 100 | +In this example, the `called()` method returns `false` because the `/users` route has not been called yet. The matching is done using the same comparison logic as if a `fetch()` request was made to the route. |
| 101 | + |
| 102 | +### `uncalledRoutes` |
| 103 | + |
| 104 | +The `uncalledRoutes` property returns an array with information about uncalled routes. You can use this property to check if any routes have not been called: |
| 105 | + |
| 106 | +```js {12} |
| 107 | +import { MockServer, FetchMocker } from "mentoss"; |
| 108 | + |
| 109 | +const server1 = new MockServer("https://api1.example.com"); |
| 110 | +const server2 = new MockServer("https://api2.example.com"); |
| 111 | +const mocker = new FetchMocker({ servers: [server1, server2] }); |
| 112 | + |
| 113 | +server1.get("/users", 200); |
| 114 | +server2.post("/users", 200); |
| 115 | + |
| 116 | +await mocker.fetch("https://api1.example.com/users"); |
| 117 | + |
| 118 | +console.log(mocker.uncalledRoutes); // ["🚧 [Route: POST https://api2.example.com/users -> 200]"] |
| 119 | +``` |
| 120 | + |
| 121 | +The `uncalledRoutes` property is helpful when you want to format your own error messages or assertions. |
| 122 | + |
| 123 | +<Aside type="tip"> |
| 124 | +The `MockServer` class also supports `allRoutesCalled()`, `assertAllRoutesCalled()`, and `called()` methods, as well as the `uncalledRoutes` property. These methods and property work similarly to the `FetchMocker` equivalents except they only check routes on the given server. |
| 125 | +</Aside> |
| 126 | + |
| 127 | +## Additional Helpers |
| 128 | + |
| 129 | +### `clearAll()` |
| 130 | + |
| 131 | +The `clearAll()` method removes all routes from all servers so you can reuse the same server for multiple tests. |
| 132 | + |
| 133 | +```js {10} |
| 134 | +import { MockServer, FetchMocker } from "mentoss"; |
| 135 | + |
| 136 | +const server1 = new MockServer("https://api1.example.com"); |
| 137 | +const server2 = new MockServer("https://api2.example.com"); |
| 138 | +const mocker = new FetchMocker({ servers: [server1, server2] }); |
| 139 | + |
| 140 | +server1.get("/users", 200); |
| 141 | +server2.post("/users", 200); |
| 142 | + |
| 143 | +mocker.clearAll(); |
| 144 | + |
| 145 | +await mocker.fetch("https://api1.example.com/users"); // Error! |
| 146 | +``` |
0 commit comments