diff --git a/site/jest.config.js b/site/jest.config.js index 17e6e73fd154d..d72d368ab8efa 100644 --- a/site/jest.config.js +++ b/site/jest.config.js @@ -11,6 +11,7 @@ module.exports = { preset: "ts-jest", roots: [""], + setupFilesAfterEnv: ["./jest.setup.ts"], transform: { "^.+\\.tsx?$": "ts-jest", }, diff --git a/site/jest.setup.ts b/site/jest.setup.ts new file mode 100644 index 0000000000000..71ac0a695c1da --- /dev/null +++ b/site/jest.setup.ts @@ -0,0 +1,22 @@ +// Helper utility to fail jest tests if a console.error is logged +// Pulled from this blog post: +// https://www.benmvp.com/blog/catch-warnings-jest-tests/ + +// 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' */] + +// 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 + consoleAsAny[logType] = (message: string): void => { + throw new Error(`Failing due to console.${logType} while running test!\n\n${message}`) + } +}) + +// This is needed because we are compiling under `--isolatedModules` +export {} diff --git a/site/src/components/EmptyState/index.test.tsx b/site/src/components/EmptyState/index.test.tsx index ff75a28b08365..f7c16ee3d4674 100644 --- a/site/src/components/EmptyState/index.test.tsx +++ b/site/src/components/EmptyState/index.test.tsx @@ -11,4 +11,25 @@ describe("EmptyState", () => { // Then await screen.findByText("Hello, world") }) + + it("renders description text", async () => { + // When + render() + + // Then + await screen.findByText("Hello, world") + await screen.findByText("Friendly greeting") + }) + + it("renders description component", async () => { + // Given + const description =