Skip to content
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

feat: throw and suggest a URL polyfill for React Native #1520

Merged
merged 2 commits into from Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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".`,
),
)
}