Skip to content

Commit

Permalink
Leaves mocking enabled on failed integrity check
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 29, 2020
1 parent 574a3f4 commit eb69dcd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "msw",
"version": "0.11.2",
"description": "Deviation-less client-side runtime API mocking using Service Workers.",
"description": "Client-side API mocking using Service Workers.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"bin": {
Expand Down
8 changes: 4 additions & 4 deletions src/composeMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ const createStart = (

if (integrityError) {
console.error(`\
[MSW] Failed to activate Service Worker: integrity check didn't pass.
[MSW] Detected outdated Service Worker: ${integrityError.message}
This error most likely means that the "mockServiceWorker.js" file served by your application is older than the Service Worker module distributed in the latest release of the library. Please update your Service Worker by running the following command in your project's root directory:
The mocking is still enabled, but it's highly recommended that you update your Service Worker by running:
$ npx msw init <PUBLIC_DIR>
If this error message persists after the successful initialization of a new Service Worker, please report an issue: https://github.com/open-draft/msw/issues\
This is necessary to ensure that the Service Worker is in sync with the library to guarantee its stability.
If this message still persists after updating, please report an issue: https://github.com/open-draft/msw/issues\
`)
return
}

// Signal Service Worker to enable requests interception
Expand Down
8 changes: 6 additions & 2 deletions src/requestIntegrityCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ export const requestIntegrityCheck = (
if (type === 'INTEGRITY_CHECK_RESPONSE') {
// Compare the response from the Service Worker and the
// global variable set by webpack upon build.
if (SERVICE_WORKER_CHECKSUM !== actualChecksum) {
return reject(new Error('Integrity assertion failed'))
if (actualChecksum !== SERVICE_WORKER_CHECKSUM) {
return reject(
new Error(
`Currently active Service Worker (${actualChecksum}) is behind the latest published one (${SERVICE_WORKER_CHECKSUM}).`,
),
)
}

resolve(serviceWorker)
Expand Down
5 changes: 3 additions & 2 deletions test/msw-api/integrity-check-invalid.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { composeMocks, rest } from 'msw'

const { start } = composeMocks(
rest.get('https://api.github.com/users/:username', (req, res, ctx) => {
console.log('Request handler')
return res(ctx.json({ mocked: true }))
}),
)

// Use custom Service Worker URL for the purpose of intentionally
// registering an outdated worker. Please do not use this as an example.
start('/tmp/mockServiceWorker-outdated.js')
start('/tmp/mockServiceWorker-outdated.js', {
scope: '/',
})
16 changes: 6 additions & 10 deletions test/msw-api/integrity-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ describe('Integrity check', () => {
})

it('should throw a meaningful error in the console', async () => {
const errorMessages: string[] = []
const errors: string[] = []

api.page.on('console', function(message) {
if (message.type() === 'error') {
errorMessages.push(message.text())
errors.push(message.text())
}
})

Expand All @@ -87,24 +87,20 @@ describe('Integrity check', () => {
waitUntil: 'networkidle0',
})

const integrityError = errorMessages.find((message) => {
return message.startsWith(
`[MSW] Failed to activate Service Worker: integrity check didn't pass`,
)
const integrityError = errors.find((message) => {
return message.startsWith(`[MSW] Detected outdated Service Worker`)
})

expect(integrityError).toBeTruthy()
})

it('should not enable any mocking', async () => {
it('should still leave the mocking enabled', async () => {
const REQUEST_URL = 'https://api.github.com/users/octocat'
api.page.evaluate((url) => fetch(url), REQUEST_URL)
console.log('request fired...')
const res = await api.page.waitForResponse(REQUEST_URL)
console.log('response awaited!')
const body = await res.json()

expect(body).not.toEqual({
expect(body).toEqual({
mocked: true,
})
})
Expand Down
5 changes: 5 additions & 0 deletions test/support/spawnServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Resolved "msw" module to:
publicPath: '/',
noInfo: true,
openPage: '/test/support/index.html',
headers: {
// Allow for the test-only Service Workers from "/tmp" directory
// to be registered at the website's root.
'Service-Worker-Allowed': '/',
},
after(app) {
app.get('/mockServiceWorker.js', (req, res) => {
res.sendFile(path.resolve(__dirname, '../../lib/mockServiceWorker.js'))
Expand Down

0 comments on commit eb69dcd

Please sign in to comment.