-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fei5850.2.hardfail] Add hard fail support to request mocking (#2334)
## Summary: An oft annoying but sometimes useful feature of Apollo's `MockLink` is that it will hard fail if a request is made that has not been mocked. This is annoying because it forces folks to provide a mock for every request, even if they don't care about that request. However, it is useful when debugging tests to see why a mock isn't matching as expected or what requests are happening (the KA_LOG_FETCHES is useful for seeing this info too, but less accessible). This change adds the ability to turn on hard fails for our request mocking system. I have added this using a fluent API so that it is more easily worked into existing uses of this mocking system. I've also added tests to verify the new functionality, and added some additional commentary to the types for the mocking framework. Issue: FEI-5850 ## Test plan: `yarn test` `yarn typecheck` Author: somewhatabstract Reviewers: jeresig Required Reviewers: Approved By: jeresig Checks: ⌛ Publish npm snapshot (ubuntu-latest, 20.x), ⌛ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ⌛ gerald, ⌛ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ⏭️ dependabot Pull Request URL: #2334
- Loading branch information
1 parent
eb807af
commit 16565a8
Showing
12 changed files
with
429 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-testing-core": minor | ||
"@khanacademy/wonder-blocks-testing": minor | ||
--- | ||
|
||
Add support for hard fails to the request mocking features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,65 @@ | ||
import type {MockResponse} from "../respond-with"; | ||
import {ConfigureFn} from "../types"; | ||
|
||
export type FetchMockOperation = RegExp | string; | ||
|
||
type FetchMockOperationFn = ( | ||
operation: FetchMockOperation, | ||
response: MockResponse<any>, | ||
) => FetchMockFn; | ||
interface FetchMockOperationFn { | ||
( | ||
/** | ||
* The operation to match. | ||
* | ||
* This is a string for an exact match, or a regex. This is compared to | ||
* to the URL of the fetch request to determine if it is a matching | ||
* request. | ||
*/ | ||
operation: FetchMockOperation, | ||
|
||
/** | ||
* The response to return when the operation is matched. | ||
*/ | ||
response: MockResponse<any>, | ||
): FetchMockFn; | ||
} | ||
|
||
export type FetchMockFn = { | ||
/** | ||
* The mock fetch function. | ||
* | ||
* This function is a drop-in replacement for the fetch function. You should | ||
* not need to call this function directly. Just replace the normal fetch | ||
* function implementation with this. | ||
*/ | ||
(input: RequestInfo, init?: RequestInit): Promise<Response>; | ||
|
||
/** | ||
* Mock a fetch operation. | ||
* | ||
* This adds a response for a given mocked operation. Regardless of how | ||
* many times this mock is matched, it will be used. | ||
* | ||
* @returns The mock fetch function for chaining. | ||
*/ | ||
mockOperation: FetchMockOperationFn; | ||
|
||
/** | ||
* Mock a fetch operation once. | ||
* | ||
* This adds a response for a given mocked operation that will only be used | ||
* once and discarded. | ||
* | ||
* @returns The mock fetch function for chaining. | ||
*/ | ||
mockOperationOnce: FetchMockOperationFn; | ||
|
||
/** | ||
* Configure the mock fetch function with the given configuration. | ||
* | ||
* This function is provided as a convenience to allow for configuring the | ||
* mock fetch function in a fluent manner. The configuration is applied | ||
* to all mocks for a given fetch function; the last configuration applied | ||
* will be the one that is used for all mocked operations. | ||
* | ||
* @returns The mock fetch function for chaining. | ||
*/ | ||
configure: ConfigureFn<FetchMockOperation, any>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.