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 service worker scope and worker script URL to "Mocking enabled" message #1172

Merged
merged 2 commits into from
Apr 4, 2022
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
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')}`,
)
})