Skip to content

Commit

Permalink
fix: output a single formatted message to console (#1820)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Nov 1, 2023
1 parent a179b68 commit 2ead392
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/core/handlers/GraphQLHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ Consider naming this operation or using "graphql.operation()" request handler to
: `anonymous ${args.parsedResult?.operationType}`

console.groupCollapsed(
devUtils.formatMessage('%s %s (%c%s%c)'),
getTimestamp(),
`${requestInfo}`,
devUtils.formatMessage(
`${getTimestamp()} ${requestInfo} (%c${loggedResponse.status} ${
loggedResponse.statusText
}%c)`,
),
`color:${statusColor}`,
`${loggedResponse.status} ${loggedResponse.statusText}`,
'color:inherit',
)
console.log('Request:', loggedRequest)
Expand Down
10 changes: 5 additions & 5 deletions src/core/handlers/HttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ export class HttpHandler extends RequestHandler<
const statusColor = getStatusCodeColor(loggedResponse.status)

console.groupCollapsed(
devUtils.formatMessage('%s %s %s (%c%s%c)'),
getTimestamp(),
args.request.method,
publicUrl,
devUtils.formatMessage(
`${getTimestamp()} ${args.request.method} ${publicUrl} (%c${
loggedResponse.status
} ${loggedResponse.statusText}%c)`,
),
`color:${statusColor}`,
`${loggedResponse.status} ${loggedResponse.statusText}`,
'color:inherit',
)
console.log('Request', loggedRequest)
Expand Down
8 changes: 4 additions & 4 deletions test/browser/graphql-api/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('prints a log for a GraphQL query', async ({
expect.arrayContaining([
expect.stringMatching(
new RegExp(
`^\\[MSW\\] %s %s \\(%c%s%c\\) \\d{2}:\\d{2}:\\d{2} query GetUserDetail color:${StatusCodeColor.Success} 200 OK color:inherit$`,
`^\\[MSW\\] \\d{2}:\\d{2}:\\d{2} query GetUserDetail \\(%c200 OK%c\\) color:${StatusCodeColor.Success} color:inherit$`,
),
),
]),
Expand Down Expand Up @@ -60,7 +60,7 @@ test('prints a log for a GraphQL mutation', async ({
expect.arrayContaining([
expect.stringMatching(
new RegExp(
`\\[MSW\\] %s %s \\(%c%s%c\\) \\d{2}:\\d{2}:\\d{2} mutation Login color:${StatusCodeColor.Success} 200 OK color:inherit$`,
`\\[MSW\\] \\d{2}:\\d{2}:\\d{2} mutation Login \\(%c200 OK%c\\) color:${StatusCodeColor.Success} color:inherit$`,
),
),
]),
Expand Down Expand Up @@ -91,7 +91,7 @@ test('prints a log for a GraphQL query intercepted via "graphql.operation"', asy
expect.arrayContaining([
expect.stringMatching(
new RegExp(
`\\[MSW\\] %s %s \\(%c%s%c\\) \\d{2}:\\d{2}:\\d{2} query GetLatestPosts color:${StatusCodeColor.Warning} 301 Moved Permanently color:inherit$`,
`\\[MSW\\] \\d{2}:\\d{2}:\\d{2} query GetLatestPosts \\(%c301 Moved Permanently%c\\) color:${StatusCodeColor.Warning} color:inherit$`,
),
),
]),
Expand Down Expand Up @@ -122,7 +122,7 @@ test('prints a log for a GraphQL mutation intercepted via "graphql.operation"',
expect.arrayContaining([
expect.stringMatching(
new RegExp(
`\\[MSW\\] %s %s \\(%c%s%c\\) \\d{2}:\\d{2}:\\d{2} mutation CreatePost color:${StatusCodeColor.Warning} 301 Moved Permanently color:inherit$`,
`^\\[MSW\\] \\d{2}:\\d{2}:\\d{2} mutation CreatePost \\(%c301 Moved Permanently%c\\) color:${StatusCodeColor.Warning} color:inherit$`,
),
),
]),
Expand Down
4 changes: 2 additions & 2 deletions test/browser/rest-api/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ test('prints the intercepted request info into browser console', async ({
await fetch('https://example.com/users/octocat')

await waitFor(() => {
expect(consoleSpy.get('raw')?.get('startGroupCollapsed')).toEqual(
expect(consoleSpy.get('raw')!.get('startGroupCollapsed')).toEqual(
expect.arrayContaining([
expect.stringMatching(
new RegExp(
`^\\[MSW\\] %s %s %s \\(%c%s%c\\) \\d{2}:\\d{2}:\\d{2} GET https://example.com/users/octocat color:${StatusCodeColor.Success} 200 OK color:inherit$`,
`^\\[MSW\\] \\d{2}:\\d{2}:\\d{2} GET https://example.com/users/octocat \\(%c200 OK%c\\) color:${StatusCodeColor.Success} color:inherit$`,
),
),
]),
Expand Down

0 comments on commit 2ead392

Please sign in to comment.