1- import { testWriteStream } from './utils' ;
2- import { setContext } from '@sentry/core' ;
3-
41let loggerFactory ;
5- const captureException = jest . fn ( ) ;
6- const captureMessage = jest . fn ( ) ;
2+ const scope : any = { } ;
3+ const withScope = jest . fn ( fn =>
4+ fn ( {
5+ setContext : ( key : string , val : any ) => {
6+ scope . context = { [ key ] : val } ;
7+ } ,
8+ setLevel : ( level : any ) => {
9+ scope . level = level ;
10+ } ,
11+ } )
12+ ) ;
13+
14+ const createCapture = ( cb = ( ) => { } ) => data => {
15+ cb ( ) ;
16+ return { data, scope } ;
17+ } ;
18+
19+ const captureException = jest . fn ( createCapture ( ) ) ;
20+ const captureMessage = jest . fn ( createCapture ( ) ) ;
721
822describe ( 'sentry mocked' , ( ) => {
923 beforeAll ( ( ) => {
1024 jest . mock ( '@sentry/node' , ( ) => {
1125 return {
1226 captureException,
27+ withScope,
28+ Severity : {
29+ Debug : 'debug' ,
30+ Info : 'info' ,
31+ Warning : 'warning' ,
32+ Error : 'error' ,
33+ Critical : 'critical' ,
34+ } ,
1335 } ;
1436 } ) ;
1537 loggerFactory = require ( '..' ) . default ;
@@ -19,15 +41,12 @@ describe('sentry mocked', () => {
1941 expect ( ( ) => loggerFactory ( { sentryDsn : 'DSN' } ) ) . not . toThrowError ( ) ;
2042 } ) ;
2143
22- test ( 'can use custom stream ' , async ( ) => {
44+ test ( 'sentry is called with correct scope ' , async ( ) => {
2345 await new Promise ( ( resolve , reject ) => {
2446 const logger = loggerFactory ( {
2547 sentryDsn : 'DSN' ,
2648 } ) ;
27- captureException . mockImplementation ( x => {
28- resolve ( ) ;
29- console . log ( { captureException : x } ) ;
30- } ) ;
49+ captureException . mockImplementation ( createCapture ( resolve ) ) ;
3150 logger . info ( 'Foo' ) ;
3251 } ) ;
3352 expect ( captureException . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
@@ -39,5 +58,24 @@ describe('sentry mocked', () => {
3958 },
4059 ]
4160 ` ) ;
61+ expect ( captureException . mock . results [ 0 ] . value ) . toMatchInlineSnapshot ( `
62+ Object {
63+ "data": Object {
64+ "level": 30,
65+ "message": "Foo",
66+ "v": 1,
67+ },
68+ "scope": Object {
69+ "context": Object {
70+ "data": Object {
71+ "level": 30,
72+ "message": "Foo",
73+ "v": 1,
74+ },
75+ },
76+ "level": "info",
77+ },
78+ }
79+ ` ) ;
4280 } ) ;
4381} ) ;
0 commit comments