From 87a7cbdb6833cdd38b712eb7caaf52979f1e3e0f Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Thu, 21 Oct 2021 14:14:35 +0200 Subject: [PATCH] replace msw with setupRequestMockHandlers Signed-off-by: Johan Haals --- packages/test-utils-core/api-report.md | 2 -- packages/test-utils/api-report.md | 11 ++++++++--- .../test-utils/src/testUtils/msw/index.ts | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/test-utils-core/api-report.md b/packages/test-utils-core/api-report.md index 60d4b5b0f7..a72e5d3038 100644 --- a/packages/test-utils-core/api-report.md +++ b/packages/test-utils-core/api-report.md @@ -14,8 +14,6 @@ export type CollectedLogs = { [key in T]: string[]; }; -// Warning: (tsdoc-missing-deprecation-message) The @deprecated block must include a deprecation message, e.g. describing the recommended alternative -// // @public @deprecated (undocumented) export class Keyboard { constructor( diff --git a/packages/test-utils/api-report.md b/packages/test-utils/api-report.md index b432141fa1..97fdbcc2d4 100644 --- a/packages/test-utils/api-report.md +++ b/packages/test-utils/api-report.md @@ -146,9 +146,7 @@ export type MockStorageBucket = { [key: string]: any; }; -// Warning: (ae-missing-release-tag) "msw" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) +// @public @deprecated (undocumented) export const msw: { setupDefaultHandlers: (worker: { listen: (t: any) => void; @@ -166,6 +164,13 @@ export function renderInTestApp( // @public export function renderWithEffects(nodes: ReactElement): Promise; +// @public +export function setupRequestMockHandlers(worker: { + listen: (t: any) => void; + close: () => void; + resetHandlers: () => void; +}): void; + // @public export type SyncLogCollector = () => void; diff --git a/packages/test-utils/src/testUtils/msw/index.ts b/packages/test-utils/src/testUtils/msw/index.ts index 337c2999fc..f4777e9ee5 100644 --- a/packages/test-utils/src/testUtils/msw/index.ts +++ b/packages/test-utils/src/testUtils/msw/index.ts @@ -14,6 +14,10 @@ * limitations under the License. */ +/** + * @deprecated use {@link setupRequestMockHandlers} instead which can be called directly with the worker. + * @public + */ export const msw = { setupDefaultHandlers: (worker: { listen: (t: any) => void; @@ -25,3 +29,18 @@ export const msw = { afterEach(() => worker.resetHandlers()); }, }; + +/** + * Sets up handlers for request mocking + * @public + * @param worker - service worker + */ +export function setupRequestMockHandlers(worker: { + listen: (t: any) => void; + close: () => void; + resetHandlers: () => void; +}) { + beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); + afterAll(() => worker.close()); + afterEach(() => worker.resetHandlers()); +}