Skip to content

Commit e728a34

Browse files
authored
fix(lazy): avoid crash on using mocked instance after real one loaded (#606)
1 parent c260565 commit e728a34

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/templates/plugin.lazy.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Vue from 'vue'
22

33
<% if (options.lazy.injectMock) { %>
4+
/* eslint-disable-next-line quotes, comma-spacing */
5+
const API_METHODS = <%= JSON.stringify(options.lazy.mockApiMethods)%>
46
let delayedCalls = []
57
let SentryMock = {}
68
<% } %>
@@ -41,9 +43,7 @@ Vue.config.errorHandler = (error, vm, info) => {
4143

4244
export default function SentryPlugin (ctx, inject) {
4345
<% if (options.lazy.injectMock) { %>
44-
/* eslint-disable-next-line quotes, comma-spacing */
45-
const apiMethods = <%= JSON.stringify(options.lazy.mockApiMethods)%>
46-
apiMethods.forEach((key) => {
46+
API_METHODS.forEach((key) => {
4747
SentryMock[key] = (...args) => delayedCalls.push([key, args])
4848
})
4949

@@ -141,8 +141,12 @@ async function loadSentry (ctx, inject) {
141141

142142
// help gc
143143
<% if (options.lazy.injectMock) { %>
144-
// Dont unset delayedCalls & SentryMock during
145-
// development, this will cause HMR issues
144+
// Avoid crashes in case the reference to the mocked object is being used after the actual Sentry instance has loaded.
145+
API_METHODS.forEach((key) => {
146+
SentryMock[key] = (...args) => SentrySdk[key].apply(SentrySdk, args)
147+
})
148+
149+
// Dont unset delayedCalls & SentryMock during development - this will cause HMR issues.
146150
<% if (!options.dev) { %>
147151
delayedCalls = undefined
148152
SentryMock = undefined

test/fixture/lazy/pages/index.vue

+3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ export default {
2424
},
2525
created () {
2626
if (process.client) {
27+
const mockedSentry = this.$sentry
2728
this.$sentryReady().then(() => {
2829
this.isSentryReady = true
2930
console.info('Sentry is ready')
31+
// Verify that it doesn't crash.
32+
mockedSentry.captureMessage('test')
3033
})
3134
}
3235
},

test/lazy.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ describe('Smoke test (lazy)', () => {
3939
page.on('pageerror', (error) => {
4040
errors.push(error.message)
4141
})
42+
43+
const consoleMessages: string[] = []
44+
page.on('console', (message) => {
45+
consoleMessages.push(message.text())
46+
})
47+
4248
await page.goto(url('/'))
4349

4450
// process.sentry is not initialized in webpack context in tests.
4551
// expect(await $$('#server-side', page)).toBe('Works!')
4652
expect(await $$('#client-side', page)).toBe('Works and is ready!')
4753
expect(errors).toEqual([])
54+
expect(consoleMessages).toEqual(['Sentry is ready'])
4855
})
4956
})

0 commit comments

Comments
 (0)