-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add toWarnDev custom Jest matcher (#3574)
- Loading branch information
1 parent
9f84c67
commit adebe38
Showing
12 changed files
with
153 additions
and
43 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
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
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 |
---|---|---|
@@ -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(); | ||
}).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 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 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 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 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 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 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 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 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 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