Skip to content

Instantly share code, notes, and snippets.

@koba04
Last active October 28, 2015 10:44
Show Gist options
  • Save koba04/afda6fdd4211bf0fc030 to your computer and use it in GitHub Desktop.
Save koba04/afda6fdd4211bf0fc030 to your computer and use it in GitHub Desktop.
a minimum fake implementation of fetch on Node environment
import assign from 'object-assign';
export default function fakeFetch(res) {
global.fetch = (url, params) => {
return Promise.resolve(assign({}, res, {__arg: {url, params}}));
};
}
import fakeFetch from './fakeFetch';
describe('api', () => {
it('should be result is ok', done => {
fakeFetch({
status: 200,
json: () => ({res: 'ok'})
});
fetch('/api', {user: 1}).then(res => {
assert(res.status === 200);
assert(res.json().res === 'ok');
assert(res.__arg.url === '/api');
assert(res.__arg.params.user === 1);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment