Skip to content

Commit

Permalink
fix: output a single formatter message to console
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 1, 2023
1 parent 9e9c5a1 commit 29fdec7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
22 changes: 16 additions & 6 deletions src/core/handlers/GraphQLHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,22 @@ 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}`,
`color:${statusColor}`,
`${loggedResponse.status} ${loggedResponse.statusText}`,
'color:inherit',
devUtils.formatMessage(
`${getTimestamp()} ${requestInfo} (%c${loggedResponse.status} ${
loggedResponse.statusText
}%c)`,
`color:${statusColor}`,
'color:inherit',
),

// devUtils.formatMessage(
// '%s %s (%c%s%c)',
// getTimestamp(),
// `${requestInfo}`,
// `color:${statusColor}`,
// `${loggedResponse.status} ${loggedResponse.statusText}`,
// 'color:inherit',
// ),
)
console.log('Request:', loggedRequest)
console.log('Handler:', this)
Expand Down
22 changes: 15 additions & 7 deletions src/core/handlers/HttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,21 @@ 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,
`color:${statusColor}`,
`${loggedResponse.status} ${loggedResponse.statusText}`,
'color:inherit',
devUtils.formatMessage(
`${getTimestamp()} ${args.request.method} ${publicUrl} (%c${
loggedResponse.status
} ${loggedResponse.statusText}%c)`,
`color:${statusColor}`,
'color:inherit',
),

// '%s %s %s (%c%s%c)',
// getTimestamp(),
// args.request.method,
// publicUrl,
// `color:${statusColor}`,
// `${loggedResponse.status} ${loggedResponse.statusText}`,
// 'color:inherit',
)
console.log('Request', loggedRequest)
console.log('Handler:', this)
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 29fdec7

Please sign in to comment.