-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(test-setup): add custom matchers for ui logger #923
base: main
Are you sure you want to change the base?
Conversation
Code PushUp🤨 Code PushUp report has both improvements and regressions – compared target commit c440d62 with source commit 1686dda. 🕵️ See full comparison in Code PushUp portal 🔍 🏷️ Categories👎 2 groups regressed, 👍 4 audits improved, 👎 6 audits regressed, 10 audits changed without impacting score🗃️ Groups
16 other groups are unchanged. 🛡️ Audits
576 other audits are unchanged. |
expect(ui()).toHaveLoggedLevel('info'); | ||
expect(ui()).toHaveLoggedMessage( | ||
`Reports diff written to ${bold( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would merge the level and message assertions into one. Apart from more convenient usage (IMHO), it will also ensure the same level-message pair is being checked.
expect(ui()).toHaveLoggedLevel('info'); | |
expect(ui()).toHaveLoggedMessage( | |
`Reports diff written to ${bold( | |
expect(ui()).toHaveLogged( | |
'info', | |
`Reports diff written to ${bold( |
expect(ui()).toHaveLoggedNthLevel(1, 'info'); | ||
expect(ui()).toHaveLoggedNthLevel(2, 'info'); | ||
expect(ui()).toHaveLoggedNthMessage( | ||
1, | ||
'The --skipPlugins argument removed the following categories: c1, c2.', | ||
); | ||
expect(ui()).toHaveLoggedNthMessage( | ||
2, | ||
'The --onlyPlugins argument removed the following categories: c1, c2.', | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following up on my previous comment, I would merge the Nth
assertions also:
expect(ui()).toHaveLoggedNthLevel(1, 'info'); | |
expect(ui()).toHaveLoggedNthLevel(2, 'info'); | |
expect(ui()).toHaveLoggedNthMessage( | |
1, | |
'The --skipPlugins argument removed the following categories: c1, c2.', | |
); | |
expect(ui()).toHaveLoggedNthMessage( | |
2, | |
'The --onlyPlugins argument removed the following categories: c1, c2.', | |
); | |
expect(ui()).toHaveLoggedNth( | |
1, | |
'info', | |
'The --skipPlugins argument removed the following categories: c1, c2.', | |
); | |
expect(ui()).toHaveLoggedNth( | |
2, | |
'info', | |
'The --onlyPlugins argument removed the following categories: c1, c2.', | |
); |
expect(ui()).not.toHaveLoggedMessageContaining('"$0":'); | ||
expect(ui()).not.toHaveLoggedMessageContaining('"_":'); | ||
|
||
expect(log).toContain('"outputDir": "destinationDir"'); | ||
expect(log).not.toContain('"output-dir":'); | ||
expect(ui()).toHaveLoggedMessageContaining('"outputDir": "destinationDir"'); | ||
expect(ui()).not.toHaveLoggedMessageContaining('"output-dir":'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use expect.stringContaining('...')
here instead? 🤔 Would be cool if we didn't need a specialized matcher for that.
Supporting the built-in string matchers would be most flexible, e.g. to use regexes instead of substrings we'd just use expect.stringMatching(/.../)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great idea! It turned out to be easier to implement than I expected 😁
const levels = actual.logger | ||
.getRenderer() | ||
.getLogs() | ||
.map(({ message }) => extractLevel(message)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual.logger.getRenderer().getLogs().map(({ message }) => message)
logic could be extracted to a helper function.
export function extractLevel(log: string): LogLevel | null { | ||
const match = removeColorCodes(log).match(/^\[\s*\w+\((?<level>\w+)\)\s*]/); | ||
const level = match?.groups?.['level'] as LogLevel | undefined; | ||
return level && LOG_LEVELS.has(level) ? level : null; | ||
} | ||
|
||
export function extractMessage(log: string): ExtractedMessage { | ||
const match = log.match( | ||
/^\[\s*\w+\((?<level>\w+)\)\s*]\s*(?<message>.+?(\.\s*)?)$/, | ||
); | ||
const styledMessage = match?.groups?.['message'] ?? log; | ||
const unstyledMessage = removeColorCodes(styledMessage); | ||
return { styledMessage, unstyledMessage }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very robust 👍 Also well tested 🙂
60d7b79
to
7363d50
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice testing helper!
An idea I had was also providing additional matcher for severity:
toHaveLoggedWarning
toHaveNthLoggedWarning
- ...
This might exceed the scope of this PR so consider it as am idea.
I approved the PR.
Closes #552
This PR adds custom matchers for
ui()
logger.