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
Provide a way to easily mock fetch and Web platform polyfills #295
Changes from 1 commit
3974c4d
ddec5f9
e20800d
33d645f
bf8f002
2bca95f
7d12565
77ed723
2893995
dc716fe
f138a1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
import global from './global'; | ||
import dojoGlobal from './global'; | ||
`!has('build-elide')`; | ||
import 'whatwg-fetch'; | ||
import 'isomorphic-fetch'; | ||
|
||
export default global.fetch.bind(global) as (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
import has from '../has/has'; | ||
|
||
if (typeof global !== 'undefined' && (global as any).fetch && (global as any).fetch !== dojoGlobal.fetch) { | ||
dojoGlobal.fetch = (global as any).fetch; | ||
} | ||
|
||
const _fetch = dojoGlobal.fetch.bind(dojoGlobal) as (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
let replacement: (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
|
||
export default function fetch(input: RequestInfo, init?: RequestInit) { | ||
return replacement ? replacement(input, init) : _fetch(input, init); | ||
} | ||
|
||
export function replace(fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>) { | ||
if (has('test')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! |
||
replacement = fetch; | ||
} else { | ||
throw new Error('Replacement functionality is only available in a test environment'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cross-fetch
might be a better option for this (I haven't used either): https://github.com/lquixada/cross-fetch#why-not-isomorphic-fetchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I've updated this to use
cross-fetch