Skip to content

Commit

Permalink
feat: remove the ".printHandlers()" method
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jul 20, 2023
1 parent 47fd803 commit 61f6f3b
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 259 deletions.
12 changes: 12 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ To help you navigate, we've structured this guide on the feature basis. You can
- [ctx.delay](#ctxdelay)
- [ctx.fetch](#ctx-fetch)
- [Life-cycle events](#life-cycle-events)
- [`.printHandlers()`](#print-handlers)
- [Advanced](#advanced)
- [**What's new in this release?**](#whats-new)
- [Common issues](#common-issues)
Expand Down Expand Up @@ -514,6 +515,17 @@ worker.events.on('response:mocked', ({ response, request, requestId }) => {
---
## `.printHandlers()

The `worker.prinHandlers()` and `server.printHandlers()` methods were removed. Use the `.listHandlers()` method instead:

```diff
-worker.printHandlers()
+console.log(worker.listHandlers())
```

---

## Advanced

It is still possible to create custom handlers and resolvers, just make sure to account for the new [resolver call signature](#response-resolver).
Expand Down
6 changes: 0 additions & 6 deletions src/browser/setupWorker/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,5 @@ export interface SetupWorker {
*/
listHandlers(): ReadonlyArray<RequestHandler<RequestHandlerDefaultInfo, any>>

/**
* Lists all active request handlers.
* @see {@link https://mswjs.io/docs/api/setup-worker/print-handlers `worker.printHandlers()`}
*/
printHandlers: () => void

events: LifeCycleEventEmitter<LifeCycleEventsMap>
}
20 changes: 0 additions & 20 deletions src/browser/setupWorker/setupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,6 @@ export class SetupWorkerApi
return await this.startHandler(this.context.startOptions, options)
}

public printHandlers(): void {
const handlers = this.listHandlers()

handlers.forEach((handler) => {
const { header, callFrame } = handler.info
const pragma = handler.info.hasOwnProperty('operationType')
? '[graphql]'
: '[rest]'

console.groupCollapsed(`${pragma} ${header}`)

if (callFrame) {
console.log(`Declaration: ${callFrame}`)
}

console.log('Handler:', handler)
console.groupEnd()
})
}

public stop(): void {
super.dispose()
this.context.events.removeAllListeners()
Expand Down
2 changes: 0 additions & 2 deletions src/core/SetupApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,4 @@ export abstract class SetupApi<EventsMap extends EventMap> extends Disposable {
},
}
}

abstract printHandlers(): void
}
32 changes: 16 additions & 16 deletions src/core/utils/internal/getCallFrame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('supports Node.js (Linux, MacOS) error stack', () => {
'Error: ',
' at getCallFrame (/Users/mock/github/msw/lib/node/index.js:3735:22)',
' at Object.get (/Users/mock/github/msw/lib/node/index.js:3776:29)',
' at Object.<anonymous> (/Users/mock/github/msw/test/msw-api/setup-server/printHandlers.test.ts:13:8)', // <-- this one
' at Object.<anonymous> (/Users/mock/github/msw/test/msw-api/setup-server/listHandlers.test.ts:13:8)', // <-- this one
' at Runtime._execModule (/Users/mock/github/msw/node_modules/jest-runtime/build/index.js:1299:24)',
' at Runtime._loadModule (/Users/mock/github/msw/node_modules/jest-runtime/build/index.js:898:12)',
' at Runtime.requireModule (/Users/mock/github/msw/node_modules/jest-runtime/build/index.js:746:10)',
Expand All @@ -24,15 +24,15 @@ test('supports Node.js (Linux, MacOS) error stack', () => {
' at runTest (/Users/mock/github/msw/node_modules/jest-runner/build/runTest.js:472:34)',
])
expect(getCallFrame(linuxError)).toEqual(
'/Users/mock/github/msw/test/msw-api/setup-server/printHandlers.test.ts:13:8',
'/Users/mock/github/msw/test/msw-api/setup-server/listHandlers.test.ts:13:8',
)

const macOsError = new ErrorWithStack([
'Error: ',
' at getCallFrame (/Users/mock/git/msw/lib/node/index.js:3735:22)',
' at graphQLRequestHandler (/Users/mock/git/msw/lib/node/index.js:7071:25)',
' at Object.query (/Users/mock/git/msw/lib/node/index.js:7182:18)',
' at Object.<anonymous> (/Users/mock/git/msw/test/msw-api/setup-server/printHandlers.test.ts:14:11)', // <-- this one
' at Object.<anonymous> (/Users/mock/git/msw/test/msw-api/setup-server/listHandlers.test.ts:14:11)', // <-- this one
' at Runtime._execModule (/Users/mock/git/msw/node_modules/jest-runtime/build/index.js:1299:24)',
' at Runtime._loadModule (/Users/mock/git/msw/node_modules/jest-runtime/build/index.js:898:12)',
' at Runtime.requireModule (/Users/mock/git/msw/node_modules/jest-runtime/build/index.js:746:10)',
Expand All @@ -42,7 +42,7 @@ test('supports Node.js (Linux, MacOS) error stack', () => {
])

expect(getCallFrame(macOsError)).toEqual(
'/Users/mock/git/msw/test/msw-api/setup-server/printHandlers.test.ts:14:11',
'/Users/mock/git/msw/test/msw-api/setup-server/listHandlers.test.ts:14:11',
)
})

Expand All @@ -52,7 +52,7 @@ test('supports Node.js (Windows) error stack', () => {
' at getCallFrame (C:\\Users\\mock\\git\\msw\\lib\\node\\index.js:3735:22)',
' at graphQLRequestHandler (C:\\Users\\mock\\git\\msw\\lib\\node\\index.js:7071:25)',
' at Object.query (C:\\Users\\mock\\git\\msw\\lib\\node\\index.js:7182:18)',
' at Object.<anonymous> (C:\\Users\\mock\\git\\msw\\test\\msw-api\\setup-server\\printHandlers.test.ts:75:13)', // <-- this one
' at Object.<anonymous> (C:\\Users\\mock\\git\\msw\\test\\msw-api\\setup-server\\listHandlers.test.ts:75:13)', // <-- this one
' at Object.asyncJestTest (C:\\Users\\mock\\git\\msw\\node_modules\\jest-jasmine2\\build\\jasmineAsyncInstall.js:106:37)',
' at C:\\Users\\mock\\git\\msw\\node_modules\\jest-jasmine2\\build\\queueRunner.js:45:12',
' at new Promise (<anonymous>)',
Expand All @@ -61,7 +61,7 @@ test('supports Node.js (Windows) error stack', () => {
])

expect(getCallFrame(error)).toBe(
'C:\\Users\\mock\\git\\msw\\test\\msw-api\\setup-server\\printHandlers.test.ts:75:13',
'C:\\Users\\mock\\git\\msw\\test\\msw-api\\setup-server\\listHandlers.test.ts:75:13',
)
})

Expand All @@ -70,8 +70,8 @@ test('supports Chrome and Edge error stack', () => {
'Error',
' at getCallFrame (webpack:///./lib/browser/getCallFrame-deps.js?:272:20)',
' at Object.eval [as get] (webpack:///./lib/browser/rest-deps.js?:1402:90)',
' at eval (webpack:///./test/msw-api/setup-worker/printHandlers.mocks.ts?:6:113)', // <-- this one
' at Module../test/msw-api/setup-worker/printHandlers.mocks.ts (http://localhost:59464/main.js:1376:1)',
' at eval (webpack:///./test/msw-api/setup-worker/listHandlers.mocks.ts?:6:113)', // <-- this one
' at Module../test/msw-api/setup-worker/listHandlers.mocks.ts (http://localhost:59464/main.js:1376:1)',
' at __webpack_require__ (http://localhost:59464/main.js:790:30)',
' at fn (http://localhost:59464/main.js:101:20)',
' at eval (webpack:///multi_(webpack)-dev-server/client?:4:18)',
Expand All @@ -81,16 +81,16 @@ test('supports Chrome and Edge error stack', () => {
])

expect(getCallFrame(error)).toBe(
'webpack:///./test/msw-api/setup-worker/printHandlers.mocks.ts?:6:113',
'webpack:///./test/msw-api/setup-worker/listHandlers.mocks.ts?:6:113',
)
})

test('supports Firefox (MacOS, Windows) error stack', () => {
const error = new ErrorWithStack([
'getCallFrame@webpack:///./lib/browser/getCallFrame-deps.js?:272:20',
'createRestHandler/<@webpack:///./lib/browser/rest-deps.js?:1402:90',
'@webpack:///./test/msw-api/setup-worker/printHandlers.mocks.ts?:6:113', // <-- this one
'./test/msw-api/setup-worker/printHandlers.mocks.ts@http://localhost:59464/main.js:1376:1',
'@webpack:///./test/msw-api/setup-worker/listHandlers.mocks.ts?:6:113', // <-- this one
'./test/msw-api/setup-worker/listHandlers.mocks.ts@http://localhost:59464/main.js:1376:1',
'__webpack_require__@http://localhost:59464/main.js:790:30',
'fn@http://localhost:59464/main.js:101:20',
'@webpack:///multi_(webpack)-dev-server/client?:4:18',
Expand All @@ -100,7 +100,7 @@ test('supports Firefox (MacOS, Windows) error stack', () => {
])

expect(getCallFrame(error)).toBe(
'webpack:///./test/msw-api/setup-worker/printHandlers.mocks.ts?:6:113',
'webpack:///./test/msw-api/setup-worker/listHandlers.mocks.ts?:6:113',
)
})

Expand All @@ -110,7 +110,7 @@ test('supports Safari (MacOS) error stack', () => {
'',
'eval code',
'eval@[native code]',
'./test/msw-api/setup-worker/printHandlers.mocks.ts@http://localhost:59464/main.js:1376:5', // <-- this one
'./test/msw-api/setup-worker/listHandlers.mocks.ts@http://localhost:59464/main.js:1376:5', // <-- this one
'__webpack_require__@http://localhost:59464/main.js:790:34',
'fn@http://localhost:59464/main.js:101:39',
'eval code',
Expand All @@ -122,15 +122,15 @@ test('supports Safari (MacOS) error stack', () => {
])

expect(getCallFrame(errorOne)).toBe(
'./test/msw-api/setup-worker/printHandlers.mocks.ts@http://localhost:59464/main.js:1376:5',
'./test/msw-api/setup-worker/listHandlers.mocks.ts@http://localhost:59464/main.js:1376:5',
)

const errorTwo = new ErrorWithStack([
'getCallFrame',
'graphQLRequestHandler',
'eval code',
'eval@[native code]',
'./test/msw-api/setup-worker/printHandlers.mocks.ts@http://localhost:56460/main.js:1376:5', // <-- this one
'./test/msw-api/setup-worker/listHandlers.mocks.ts@http://localhost:56460/main.js:1376:5', // <-- this one
'__webpack_require__@http://localhost:56460/main.js:790:34',
'fn@http://localhost:56460/main.js:101:39',
'eval code',
Expand All @@ -142,7 +142,7 @@ test('supports Safari (MacOS) error stack', () => {
])

expect(getCallFrame(errorTwo)).toBe(
'./test/msw-api/setup-worker/printHandlers.mocks.ts@http://localhost:56460/main.js:1376:5',
'./test/msw-api/setup-worker/listHandlers.mocks.ts@http://localhost:56460/main.js:1376:5',
)
})

Expand Down
17 changes: 0 additions & 17 deletions src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,6 @@ export class SetupServerApi
)
}

public printHandlers(): void {
const handlers = this.listHandlers()

handlers.forEach((handler) => {
const { header, callFrame } = handler.info

const pragma = handler.info.hasOwnProperty('operationType')
? '[graphql]'
: '[rest]'

console.log(`\
${`${pragma} ${header}`}
Declaration: ${callFrame}
`)
})
}

public close(): void {
this.dispose()
}
Expand Down
6 changes: 0 additions & 6 deletions src/node/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,5 @@ export interface SetupServer {
*/
listHandlers(): ReadonlyArray<RequestHandler<RequestHandlerDefaultInfo, any>>

/**
* Lists all active request handlers.
* @see {@link https://mswjs.io/docs/api/setup-server/print-handlers `server.print-handlers()`}
*/
printHandlers(): void

events: LifeCycleEventEmitter<LifeCycleEventsMap>
}
2 changes: 2 additions & 0 deletions test/browser/msw-api/setup-worker/listHandlers.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const worker = setupWorker(
github.operation(resolver),
)

worker.start()

// @ts-ignore
window.msw = {
worker,
Expand Down
2 changes: 1 addition & 1 deletion test/browser/msw-api/setup-worker/listHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare namespace window {
}
}

const LIST_HANDLER_EXAMPLE = require.resolve('./printHandlers.mocks.ts')
const LIST_HANDLER_EXAMPLE = require.resolve('./listHandlers.mocks.ts')

test('lists all current request handlers', async ({ loadExample, page }) => {
await loadExample(LIST_HANDLER_EXAMPLE)
Expand Down
24 changes: 0 additions & 24 deletions test/browser/msw-api/setup-worker/printHandlers.mocks.ts

This file was deleted.

72 changes: 0 additions & 72 deletions test/browser/msw-api/setup-worker/printHandlers.test.ts

This file was deleted.

Loading

0 comments on commit 61f6f3b

Please sign in to comment.