Skip to content

refactor: clean up types in jest.setup.ts #4285

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

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions site/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ afterAll(() => server.close())
// For now, I limited this to just 'error' - but failing on warnings
// would be a nice next step! We may need to filter out some noise
// from material-ui though.
const CONSOLE_FAIL_TYPES = ["error" /* 'warn' */]
const CONSOLE_FAIL_TYPES = ["error" /* 'warn' */] as const
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using as const narrows the type to string literals which is more correct.


// Throw errors when a `console.error` or `console.warn` happens
// by overriding the functions
CONSOLE_FAIL_TYPES.forEach((logType: string) => {
// Suppressing the no-explicit-any to override certain console functions for testing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const consoleAsAny = global.console as any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
consoleAsAny[logType] = (format: string, ...args: any[]): void => {
CONSOLE_FAIL_TYPES.forEach((logType: typeof CONSOLE_FAIL_TYPES[number]) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here logType is limited to the options we specify in CONSOLE_FAIL_TYPES.

global.console[logType] = <Type>(format: string, ...args: Type[]): void => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the generic, we no longer need to use any.

throw new Error(
`Failing due to console.${logType} while running test!\n\n${util.format(format, ...args)}`,
)
Expand Down