Skip to content

Commit

Permalink
feat: add service worker scope and worker script URL to "Mocking enab…
Browse files Browse the repository at this point in the history
…led" message (#1172)

* fix: add service worker scope and worker script url to "Mocking enabled" message

* chore(printStartMessage): remove context from the function

Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
Poivey and kettanaito committed May 17, 2022
1 parent 16fe0a2 commit ab3d399
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/setupWorker/start/utils/enableMocking.ts
Expand Up @@ -10,6 +10,10 @@ export async function enableMocking(
) {
context.workerChannel.send('MOCK_ACTIVATE')
return context.events.once('MOCKING_ENABLED').then(() => {
printStartMessage({ quiet: options.quiet })
printStartMessage({
quiet: options.quiet,
workerScope: context.registration?.scope,
workerUrl: context.worker?.scriptURL,
})
})
}
39 changes: 38 additions & 1 deletion src/setupWorker/start/utils/printStartMessage.test.ts
Expand Up @@ -14,7 +14,10 @@ afterAll(() => {
})

test('prints out a default start message into console', () => {
printStartMessage()
printStartMessage({
workerScope: 'http://localhost:3000/',
workerUrl: 'http://localhost:3000/worker.js',
})

expect(console.groupCollapsed).toHaveBeenCalledWith(
'%c[MSW] Mocking enabled.',
Expand All @@ -32,6 +35,18 @@ test('prints out a default start message into console', () => {
expect(console.log).toHaveBeenCalledWith(
'Found an issue? https://github.com/mswjs/msw/issues',
)

// Includes service worker scope.
expect(console.log).toHaveBeenCalledWith(
'Worker scope:',
'http://localhost:3000/',
)

// Includes service worker script location.
expect(console.log).toHaveBeenCalledWith(
'Worker script URL:',
'http://localhost:3000/worker.js',
)
})

test('supports printing a custom start message', () => {
Expand All @@ -49,3 +64,25 @@ test('does not print any messages when log level is quiet', () => {
expect(console.groupCollapsed).not.toHaveBeenCalled()
expect(console.log).not.toHaveBeenCalled()
})

test('prints a worker scope in the start message', () => {
printStartMessage({
workerScope: 'http://localhost:3000/user',
})

expect(console.log).toHaveBeenCalledWith(
'Worker scope:',
'http://localhost:3000/user',
)
})

test('prints a worker script url in the start message', () => {
printStartMessage({
workerUrl: 'http://localhost:3000/mockServiceWorker.js',
})

expect(console.log).toHaveBeenCalledWith(
'Worker script URL:',
'http://localhost:3000/mockServiceWorker.js',
)
})
13 changes: 12 additions & 1 deletion src/setupWorker/start/utils/printStartMessage.ts
@@ -1,8 +1,10 @@
import { devUtils } from '../../../utils/internal/devUtils'

interface PrintStartMessageArgs {
export interface PrintStartMessageArgs {
quiet?: boolean
message?: string
workerUrl?: string
workerScope?: string
}

/**
Expand All @@ -25,5 +27,14 @@ export function printStartMessage(args: PrintStartMessageArgs = {}) {
'font-weight:normal',
)
console.log('Found an issue? https://github.com/mswjs/msw/issues')

if (args.workerUrl) {
console.log('Worker script URL:', args.workerUrl)
}

if (args.workerScope) {
console.log('Worker scope:', args.workerScope)
}

console.groupEnd()
}
22 changes: 22 additions & 0 deletions test/msw-api/setup-worker/start/start.test.ts
Expand Up @@ -57,3 +57,25 @@ test('resolves the "start" Promise when the worker has been activated', async ()
expect(events[2]).toEqual('enabled message')
expect(events).toHaveLength(3)
})

test('prints the start message when the worker has been registered', async () => {
const runtime = await pageWith({
example: path.resolve(__dirname, 'start.mocks.ts'),
routes(app) {
app.get('/worker.js', (req, res) => {
res.sendFile(path.resolve(__dirname, 'worker.delayed.js'))
})
},
})

await runtime.page.evaluate(() => {
return window.msw.startWorker()
})

expect(runtime.consoleSpy.get('log')).toContain(
`Worker scope: ${runtime.makeUrl('/')}`,
)
expect(runtime.consoleSpy.get('log')).toContain(
`Worker script URL: ${runtime.makeUrl('/worker.js')}`,
)
})

0 comments on commit ab3d399

Please sign in to comment.