@@ -5,6 +5,22 @@ import * as path from 'path';
55
66const NODE_VERSION = parseSemver ( process . versions . node ) . major || 0 ;
77
8+ /** The output will contain logging so we need to find the line that parses as JSON */
9+ function parseJsonLine < T > ( input : string ) : T {
10+ return (
11+ input
12+ . split ( '\n' )
13+ . map ( line => {
14+ try {
15+ return JSON . parse ( line ) as T ;
16+ } catch {
17+ return undefined ;
18+ }
19+ } )
20+ . filter ( a => a ) as T [ ]
21+ ) [ 0 ] ;
22+ }
23+
824describe ( 'should report ANR when event loop blocked' , ( ) => {
925 test ( 'CJS' , done => {
1026 // The stack trace is different when node < 12
@@ -15,7 +31,7 @@ describe('should report ANR when event loop blocked', () => {
1531 const testScriptPath = path . resolve ( __dirname , 'basic.js' ) ;
1632
1733 childProcess . exec ( `node ${ testScriptPath } ` , { encoding : 'utf8' } , ( _ , stdout ) => {
18- const event = JSON . parse ( stdout ) as Event ;
34+ const event = parseJsonLine < Event > ( stdout ) ;
1935
2036 expect ( event . exception ?. values ?. [ 0 ] . mechanism ) . toEqual ( { type : 'ANR' } ) ;
2137 expect ( event . exception ?. values ?. [ 0 ] . type ) . toEqual ( 'ApplicationNotResponding' ) ;
@@ -42,7 +58,7 @@ describe('should report ANR when event loop blocked', () => {
4258 const testScriptPath = path . resolve ( __dirname , 'basic.mjs' ) ;
4359
4460 childProcess . exec ( `node ${ testScriptPath } ` , { encoding : 'utf8' } , ( _ , stdout ) => {
45- const event = JSON . parse ( stdout ) as Event ;
61+ const event = parseJsonLine < Event > ( stdout ) ;
4662
4763 expect ( event . exception ?. values ?. [ 0 ] . mechanism ) . toEqual ( { type : 'ANR' } ) ;
4864 expect ( event . exception ?. values ?. [ 0 ] . type ) . toEqual ( 'ApplicationNotResponding' ) ;
@@ -64,7 +80,7 @@ describe('should report ANR when event loop blocked', () => {
6480 const testScriptPath = path . resolve ( __dirname , 'forker.js' ) ;
6581
6682 childProcess . exec ( `node ${ testScriptPath } ` , { encoding : 'utf8' } , ( _ , stdout ) => {
67- const event = JSON . parse ( stdout ) as Event ;
83+ const event = parseJsonLine < Event > ( stdout ) ;
6884
6985 expect ( event . exception ?. values ?. [ 0 ] . mechanism ) . toEqual ( { type : 'ANR' } ) ;
7086 expect ( event . exception ?. values ?. [ 0 ] . type ) . toEqual ( 'ApplicationNotResponding' ) ;
0 commit comments