diff --git a/packages/docs/jest.config.js b/packages/docs/jest.config.js index 681c820..84fb4b1 100644 --- a/packages/docs/jest.config.js +++ b/packages/docs/jest.config.js @@ -12,7 +12,10 @@ module.exports = { 'jsx', 'node', ], - moduleNameMapper: { '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy' }, + moduleNameMapper: { + '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy', + '^msw-storybook-addon$': '/../msw-addon/dist/index.node.js', + }, resetMocks: true, resetModules: false, restoreMocks: false, diff --git a/packages/msw-addon/src/index.ts b/packages/msw-addon/src/index.ts index 3d085bf..0b8a9eb 100644 --- a/packages/msw-addon/src/index.ts +++ b/packages/msw-addon/src/index.ts @@ -1,3 +1,3 @@ -export { initialize, InitializeOptions } from '@build-time/initialize' +export { initialize, InitializeOptions, getWorker } from '@build-time/initialize' export * from './decorator' export * from './loader' diff --git a/packages/msw-addon/src/initialize.browser.ts b/packages/msw-addon/src/initialize.browser.ts index 5ce6569..192038a 100644 --- a/packages/msw-addon/src/initialize.browser.ts +++ b/packages/msw-addon/src/initialize.browser.ts @@ -15,3 +15,13 @@ export function initialize( api = worker return worker } + +export function getWorker(): SetupWorker { + if (api === undefined) { + throw new Error( + `[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` + ) + } + + return api +} \ No newline at end of file diff --git a/packages/msw-addon/src/initialize.node.ts b/packages/msw-addon/src/initialize.node.ts index 571c1d3..7d2725c 100644 --- a/packages/msw-addon/src/initialize.node.ts +++ b/packages/msw-addon/src/initialize.node.ts @@ -15,3 +15,13 @@ export function initialize( api = server return server } + +export function getWorker(): SetupServer { + if (api === undefined) { + throw new Error( + `[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` + ) + } + + return api +} \ No newline at end of file diff --git a/packages/msw-addon/src/initialize.react-native.ts b/packages/msw-addon/src/initialize.react-native.ts index 37a0f9f..c3ce628 100644 --- a/packages/msw-addon/src/initialize.react-native.ts +++ b/packages/msw-addon/src/initialize.react-native.ts @@ -1,5 +1,5 @@ import type { RequestHandler } from 'msw' -import { SetupServer } from 'msw/node' +import type { SetupServer } from 'msw/node' import { setupServer } from 'msw/native' export let api: SetupServer @@ -15,3 +15,13 @@ export function initialize( api = server return server } + +export function getWorker(): SetupServer { + if (api === undefined) { + throw new Error( + `[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` + ) + } + + return api +} \ No newline at end of file