-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix jest tests by mocking i18n, and prevent future such breakages by …
…making warns and errors fatal
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
// jest.setup.js | ||
import "@testing-library/jest-dom/extend-expect"; | ||
|
||
|
||
const CONSOLE_FAIL_TYPES = ['error', 'warn']; | ||
|
||
// Throw errors when a `console.error` or `console.warn` happens | ||
// by overriding the functions. | ||
// If the warning/error is intentional, then catch it and expect for it, like: | ||
// | ||
// jest.spyOn(console, 'warn').mockImplementation(); | ||
// ... | ||
// expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Empty titles are deprecated.')); | ||
CONSOLE_FAIL_TYPES.forEach((type) => { | ||
const orig_f = console[type]; | ||
console[type] = (...message) => { | ||
orig_f(...message); | ||
throw new Error(`Failing due to console.${type} while running test!\n\n${message.join(' ')}`); | ||
}; | ||
}); | ||
|
||
// Mock out useTranslation hook as per https://react.i18next.com/misc/testing | ||
jest.mock('react-i18next', () => ({ | ||
// this mock makes sure any components using the translate hook can use it without a warning being shown | ||
useTranslation: () => { | ||
return { | ||
t: (str) => str, | ||
i18n: { | ||
changeLanguage: () => new Promise(() => {}), | ||
}, | ||
}; | ||
}, | ||
initReactI18next: { | ||
type: '3rdParty', | ||
init: () => {}, | ||
} | ||
})); |