Created
March 30, 2017 09:35
-
-
Save MasashiSalvador57f/2c02f3ce98d9da287aecb5184a143dd8 to your computer and use it in GitHub Desktop.
a small suprise about fetch-mock (with whatwg-fetch) running in Jest ref: http://qiita.com/MasashiSalvador57f/items/ee6fb08498124770e13f
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
| import 'whatwg-fetch'; | |
| export const makeRequest = () => { | |
| return fetch("http://httpbin.org/get").then((response) => { | |
| const x = response.json(); | |
| return x; | |
| }); | |
| } | |
| export default { makeRequest } |
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
| import fetchMock from 'fetch-mock'; | |
| import { makeRequest } from './call_api'; | |
| beforeAll(() => { | |
| return fetchMock.get('http://httpbin.org/get', { | |
| hello: 'world', | |
| }); | |
| }); | |
| test('sample unit test using node-nock', () => { | |
| return makeRequest().then((res) => { | |
| console.log(JSON.stringify(res)); | |
| expect(JSON.stringify(res)).toBe(JSON.stringify({ | |
| hello: 'world', | |
| })); | |
| }); | |
| }); | |
| afterAll(() => { | |
| return fetchMock.restore(); | |
| }); |
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
| expect(JSON.stringify(res)).toBe(JSON.stringify({ | |
| hello: 'world', | |
| })); |
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
| expect(res).toBe({ | |
| hello: 'world', | |
| }); |
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
| expect(received).toBe(expected) | |
| Expected value to be (using ===): | |
| {"hello": "world"} | |
| Received: | |
| {"hello": "world"} | |
| Difference: | |
| Compared values have no visual difference. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment