Skip to content

Commit

Permalink
read getWorker export
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jan 29, 2024
1 parent 9892d86 commit f48a200
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/docs/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$': '<rootDir>/../msw-addon/dist/index.node.js',
},
resetMocks: true,
resetModules: false,
restoreMocks: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/msw-addon/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 10 additions & 0 deletions packages/msw-addon/src/initialize.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions packages/msw-addon/src/initialize.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion packages/msw-addon/src/initialize.react-native.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

0 comments on commit f48a200

Please sign in to comment.