Skip to content

Instantly share code, notes, and snippets.

@MasashiSalvador57f
Created March 30, 2017 09:35
Show Gist options
  • Select an option

  • Save MasashiSalvador57f/2c02f3ce98d9da287aecb5184a143dd8 to your computer and use it in GitHub Desktop.

Select an option

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
import 'whatwg-fetch';
export const makeRequest = () => {
return fetch("http://httpbin.org/get").then((response) => {
const x = response.json();
return x;
});
}
export default { makeRequest }
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();
});
expect(JSON.stringify(res)).toBe(JSON.stringify({
hello: 'world',
}));
expect(res).toBe({
hello: 'world',
});
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