-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(vue): Add transaction source to VueRouter instrumentation #5381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import * as SentryBrowser from '@sentry/browser'; | ||
|
||
import { vueRouterInstrumentation } from '../src'; | ||
import { Route } from '../src/router'; | ||
|
||
const captureExceptionSpy = jest.spyOn(SentryBrowser, 'captureException'); | ||
|
||
const mockVueRouter = { | ||
onError: jest.fn<void, [(error: Error) => void]>(), | ||
beforeEach: jest.fn<void, [(from: Route, to: Route, next: () => void) => void]>(), | ||
}; | ||
|
||
const mockStartTransaction = jest.fn(); | ||
const mockNext = jest.fn(); | ||
|
||
const testRoutes: Record<string, Route> = { | ||
initialPageloadRoute: { matched: [], params: {}, path: '', query: {} }, | ||
normalRoute1: { | ||
matched: [{ path: '/books/:bookId/chapter/:chapterId' }], | ||
params: { | ||
bookId: '12', | ||
chapterId: '3', | ||
}, | ||
path: '/books/12/chapter/3', | ||
query: { | ||
utm_source: 'google', | ||
}, | ||
}, | ||
normalRoute2: { | ||
matched: [{ path: '/accounts/:accountId' }], | ||
params: { | ||
accountId: '4', | ||
}, | ||
path: '/accounts/4', | ||
query: {}, | ||
}, | ||
namedRoute: { | ||
matched: [{ path: '/login' }], | ||
name: 'login-screen', | ||
params: {}, | ||
path: '/login', | ||
query: {}, | ||
}, | ||
unmatchedRoute: { | ||
matched: [], | ||
params: {}, | ||
path: '/e8733846-20ac-488c-9871-a5cbcb647294', | ||
query: {}, | ||
}, | ||
}; | ||
|
||
describe('vueRouterInstrumentation()', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return instrumentation that instruments VueRouter.onError', () => { | ||
// create instrumentation | ||
const instrument = vueRouterInstrumentation(mockVueRouter); | ||
|
||
// instrument | ||
instrument(mockStartTransaction); | ||
|
||
// check | ||
expect(mockVueRouter.onError).toHaveBeenCalledTimes(1); | ||
|
||
const onErrorCallback = mockVueRouter.onError.mock.calls[0][0]; | ||
|
||
const testError = new Error(); | ||
onErrorCallback(testError); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith(testError); | ||
}); | ||
|
||
it.each([ | ||
['initialPageloadRoute', 'normalRoute1', 'pageload', '/books/:bookId/chapter/:chapterId', 'route'], | ||
['normalRoute1', 'normalRoute2', 'navigation', '/accounts/:accountId', 'route'], | ||
['normalRoute2', 'namedRoute', 'navigation', 'login-screen', 'custom'], | ||
['normalRoute2', 'unmatchedRoute', 'navigation', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'], | ||
])( | ||
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s)', | ||
(fromKey, toKey, op, transactionName, transactionSource) => { | ||
// create instrumentation | ||
const instrument = vueRouterInstrumentation(mockVueRouter); | ||
|
||
// instrument | ||
instrument(mockStartTransaction, true, true); | ||
|
||
// check | ||
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1); | ||
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0]; | ||
|
||
const from = testRoutes[fromKey]; | ||
const to = testRoutes[toKey]; | ||
beforeEachCallback(to, from, mockNext); | ||
|
||
expect(mockStartTransaction).toHaveBeenCalledTimes(1); | ||
expect(mockStartTransaction).toHaveBeenCalledWith({ | ||
name: transactionName, | ||
metadata: { | ||
source: transactionSource, | ||
}, | ||
data: { | ||
params: to.params, | ||
query: to.query, | ||
}, | ||
op: op, | ||
tags: { | ||
'routing.instrumentation': 'vue-router', | ||
}, | ||
}); | ||
|
||
expect(mockNext).toHaveBeenCalledTimes(1); | ||
}, | ||
); | ||
|
||
test.each([ | ||
[undefined, 1], | ||
[false, 0], | ||
[true, 1], | ||
])( | ||
'should return instrumentation that considers the startTransactionOnPageLoad option = %p', | ||
(startTransactionOnPageLoad, expectedCallsAmount) => { | ||
// create instrumentation | ||
const instrument = vueRouterInstrumentation(mockVueRouter); | ||
|
||
// instrument | ||
instrument(mockStartTransaction, startTransactionOnPageLoad, true); | ||
|
||
// check | ||
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1); | ||
|
||
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0]; | ||
beforeEachCallback(testRoutes['normalRoute1'], testRoutes['initialPageloadRoute'], mockNext); | ||
|
||
expect(mockStartTransaction).toHaveBeenCalledTimes(expectedCallsAmount); | ||
}, | ||
); | ||
|
||
test.each([ | ||
[undefined, 1], | ||
[false, 0], | ||
[true, 1], | ||
])( | ||
'should return instrumentation that considers the startTransactionOnLocationChange option = %p', | ||
(startTransactionOnLocationChange, expectedCallsAmount) => { | ||
// create instrumentation | ||
const instrument = vueRouterInstrumentation(mockVueRouter); | ||
|
||
// instrument | ||
instrument(mockStartTransaction, true, startTransactionOnLocationChange); | ||
|
||
// check | ||
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1); | ||
|
||
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0]; | ||
beforeEachCallback(testRoutes['normalRoute2'], testRoutes['normalRoute1'], mockNext); | ||
|
||
expect(mockStartTransaction).toHaveBeenCalledTimes(expectedCallsAmount); | ||
}, | ||
); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't get tests to work because there was a type error here. I could have messed with tsconfigs but there is an upside of doing indexOf() instead of includes() which is backwards compatibility. I think includes is ES6 only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, coincidence? #5389