-
Notifications
You must be signed in to change notification settings - Fork 557
test: add toWarnDev custom Jest matcher #3574
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
test: add toWarnDev custom Jest matcher
- Loading branch information
commit 5af0d77f0e93266532c654aa1faa6f3a995588b2
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,6 +1,8 @@ | ||
| import Enzyme from 'enzyme'; | ||
| import Adapter from 'enzyme-adapter-react-16'; | ||
| import { createSerializer } from 'enzyme-to-json'; | ||
| import { toWarnDev } from './matchers'; | ||
|
|
||
| Enzyme.configure({ adapter: new Adapter() }); | ||
| expect.addSnapshotSerializer(createSerializer({ mode: 'deep' })); | ||
| expect.extend(toWarnDev); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* tslint:disable:no-console */ | ||
|
|
||
| describe('toWarnDev', () => { | ||
| describe('usage', () => { | ||
| test('fails with incorrect type of message', () => { | ||
| expect(() => { | ||
| // @ts-ignore:next-line | ||
| expect(() => {}).toWarnDev(false); | ||
| }).toThrowErrorMatchingInlineSnapshot( | ||
| `"toWarnDev() requires a parameter of type string but was given boolean."` | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| if (__DEV__) { | ||
| describe('without message', () => { | ||
| test('does not fail if called', () => { | ||
| expect(() => { | ||
| expect(() => { | ||
| console.warn('warning'); | ||
| }).toWarnDev(); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('fails if not called', () => { | ||
| expect(() => { | ||
| expect(() => {}).toWarnDev(); | ||
tkrugg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }).toThrowErrorMatchingInlineSnapshot(`"No warning recorded."`); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with message', () => { | ||
| test('does not fail with correct message', () => { | ||
| expect(() => { | ||
| expect(() => { | ||
| console.warn('warning'); | ||
| }).toWarnDev('warning'); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('fails if a warning is not correct', () => { | ||
| expect(() => { | ||
| expect(() => { | ||
| console.warn('warning'); | ||
| }).toWarnDev('another warning'); | ||
| }).toThrow(/Unexpected warning recorded./); | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as toWarnDev } from './toWarnDev'; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* tslint:disable:no-console */ | ||
| import jestDiff from 'jest-diff'; | ||
|
|
||
| declare global { | ||
| namespace jest { | ||
| interface Matchers<R> { | ||
| toWarnDev(expectedMessage?: string): R; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const matcher = { | ||
| toWarnDev: (callback: () => void, expectedMessage: string) => { | ||
| if (expectedMessage !== undefined && typeof expectedMessage !== 'string') { | ||
| throw new Error( | ||
| `toWarnDev() requires a parameter of type string but was given ${typeof expectedMessage}.` | ||
| ); | ||
| } | ||
|
|
||
| if (!__DEV__) { | ||
| callback(); | ||
|
|
||
| return { pass: true }; | ||
| } | ||
|
|
||
| const originalWarnMethod = console.warn; | ||
| let calledTimes = 0; | ||
| let actualWarning = ''; | ||
|
|
||
| console.warn = (message: string) => { | ||
| calledTimes++; | ||
| actualWarning = message; | ||
| }; | ||
|
|
||
| callback(); | ||
|
|
||
| console.warn = originalWarnMethod; | ||
|
|
||
| // Expectation without any message. | ||
| // We only check that `console.warn` was called. | ||
| if (expectedMessage === undefined && calledTimes === 0) { | ||
| return { | ||
| pass: false, | ||
| message: () => 'No warning recorded.', | ||
| }; | ||
| } | ||
|
|
||
| // Expectation with a message. | ||
| if (expectedMessage !== undefined && actualWarning !== expectedMessage) { | ||
| return { | ||
| pass: false, | ||
| message: () => `Unexpected warning recorded. | ||
|
|
||
| Difference: | ||
|
|
||
| ${jestDiff(expectedMessage, actualWarning)}`, | ||
| }; | ||
| } | ||
|
|
||
| return { pass: true }; | ||
| }, | ||
| }; | ||
|
|
||
| export default matcher; |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.