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: add dangerouslyRunInProduction flag to worker and server options #1791

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/browser/setupWorker/setupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { mergeRight } from '~/core/utils/internal/mergeRight'
import { LifeCycleEventsMap } from '~/core/sharedOptions'
import { SetupWorker } from './glossary'
import { supportsReadableStreamTransfer } from '../utils/supportsReadableStreamTransfer'
import { isProduction } from '~/core/utils/internal/isProduction'

interface Listener {
target: EventTarget
Expand Down Expand Up @@ -177,6 +178,14 @@ export class SetupWorkerApi
options,
) as SetupWorkerInternalContext['startOptions']

invariant(
!this.context.startOptions.dangerouslyRunInProduction && isProduction(),
devUtils.formatMessage(
'Failed to call "setupWorker" in a production environment. Please make sure you enable API mocking conditionally so it doesn\'t leak to production.',
),
'https://github.com/mswjs/msw/issues/1703',
scr2em marked this conversation as resolved.
Show resolved Hide resolved
)

return await this.startHandler(this.context.startOptions, options)
}

Expand Down
1 change: 1 addition & 0 deletions src/browser/setupWorker/start/utils/prepareStartHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const DEFAULT_START_OPTIONS: RequiredDeep<StartOptions> = {
quiet: false,
waitUntilReady: true,
onUnhandledRequest: 'warn',
dangerouslyRunInProduction: false,
findWorker(scriptURL, mockServiceWorkerUrl) {
return scriptURL === mockServiceWorkerUrl
},
Expand Down
4 changes: 4 additions & 0 deletions src/core/sharedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export interface SharedOptions {
* @example server.listen({ onUnhandledRequest: 'error' })
*/
onUnhandledRequest?: UnhandledRequestStrategy
/**
* A flag to enable MSW in production (NODE_ENV === 'production'), otherwise it will throw an error
*/
dangerouslyRunInProduction?: boolean
}

export type LifeCycleEventsMap = {
Expand Down
3 changes: 3 additions & 0 deletions src/core/utils/internal/isProduction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isProduction(): boolean {
return process && process.env.NODE_ENV === 'production'
}
9 changes: 9 additions & 0 deletions src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { handleRequest } from '~/core/utils/handleRequest'
import { devUtils } from '~/core/utils/internal/devUtils'
import { SetupServer, SetupServerInternalContext } from './glossary'
import { isNodeExceptionLike } from './utils/isNodeExceptionLike'
import { isProduction } from '~/core/utils/internal/isProduction'

const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
onUnhandledRequest: 'warn',
dangerouslyRunInProduction: false,
}

export class SetupServerApi
Expand Down Expand Up @@ -101,6 +103,13 @@ export class SetupServerApi
options,
) as RequiredDeep<SharedOptions>

invariant(
!this.resolvedOptions.dangerouslyRunInProduction && isProduction(),
devUtils.formatMessage(
'Failed to call "setupWorker" in a production environment. Please make sure you enable API mocking conditionally so it doesn\'t leak to production.',
),
'https://github.com/mswjs/msw/issues/1703',
)
// Apply the interceptor when starting the server.
this.interceptor.apply()

Expand Down