Skip to content

Commit

Permalink
feat: throw and suggest a URL polyfill for React Native (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 24, 2023
1 parent 2ae9fa9 commit b4ac829
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
@@ -1,4 +1,5 @@
import * as context from './context'
import { checkGlobals } from './utils/internal/checkGlobals'
export { context }

export { setupWorker } from './setupWorker/setupWorker'
Expand Down Expand Up @@ -65,3 +66,9 @@ export type {
export type { Path, PathParams, Match } from './utils/matching/matchRequestUrl'
export type { DelayMode } from './context/delay'
export { ParsedGraphQLRequest } from './utils/internal/parseGraphQLRequest'

// Validate environmental globals before executing any code.
// This ensures that the library gives user-friendly errors
// when ran in the environments that require additional polyfills
// from the end user.
checkGlobals()
17 changes: 17 additions & 0 deletions src/utils/internal/checkGlobals.ts
@@ -0,0 +1,17 @@
import { invariant } from 'outvariant'
import { devUtils } from './devUtils'

export function checkGlobals() {
/**
* MSW expects the "URL" constructor to be defined.
* It's not present in React Native so suggest a polyfill
* instead of failing silently.
* @see https://github.com/mswjs/msw/issues/1408
*/
invariant(
typeof URL !== 'undefined',
devUtils.formatMessage(
`Global "URL" class is not defined. This likely means that you're running MSW in an environment that doesn't support all Node.js standard API (e.g. React Native). If that's the case, please use an appropriate polyfill for the "URL" class, like "react-native-url-polyfill".`,
),
)
}

0 comments on commit b4ac829

Please sign in to comment.