From bfde536fa86056fa8e3c8bcf2b162b416f7cd6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Mon, 29 Jul 2019 13:55:22 +0200 Subject: [PATCH] fix(jest): Forced jest 20 (later interfere with unhandled exception warnings) Also improved code coverage --- package.json | 4 +- test/register.spec.js | 85 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 342ace2..42dd44f 100644 --- a/package.json +++ b/package.json @@ -49,14 +49,14 @@ "haxec": "^1.1.0" }, "devDependencies": { - "@types/jest": "^20", + "@types/jest": "20.0.8", "@types/node": "^12", "already": "^1.8.0", "commitizen": "^3", "concurrently": "^4.1.1", "coveralls": "^3", "cz-conventional-changelog": "^2", - "jest": "^20", + "jest": "20.0.4", "rimraf": "^2.6.3", "rollup": "^1.16.3", "semantic-release": "^15.13.18", diff --git a/test/register.spec.js b/test/register.spec.js index 60a33ea..817df49 100644 --- a/test/register.spec.js +++ b/test/register.spec.js @@ -105,4 +105,89 @@ describe( "register", ( ) => expect( linesWoLocation.length ).toBeGreaterThanOrEqual( 2 ); expect( linesWoLocation[ 0 ] ).toBe( linesWoLocation[ 1 ] ); } ) ); + + it( "Handle throwing in ", withConsoleSpy( async ( ) => + { + const err = new Error( "the error" ); + + new Promise( ( resolve, reject ) => + { + throw err; + } ); + + await triggerUnhandledWarnings( ); + + const linesWoColumns = splitLines( console.error.mock.calls[ 0 ] ) + .filter( line => line.includes( "register.spec.js" ) ) + .map( line => cutColumn( line ) ); + + const linesWoLocation = splitLines( console.error.mock.calls[ 0 ] ) + .filter( line => line.includes( "register.spec.js" ) ) + .map( line => cutLocation( line ) ); + + expect( linesWoColumns.length ).toBeGreaterThanOrEqual( 2 ); + expect( linesWoColumns[ 0 ] ).not.toBe( linesWoColumns[ 1 ] ); + + expect( linesWoLocation.length ).toBeGreaterThanOrEqual( 2 ); + expect( linesWoLocation[ 0 ] ).toBe( linesWoLocation[ 1 ] ); + } ) ); + + it( "Handle error without stack", withConsoleSpy( async ( ) => + { + const err = new Error( "the error" ); + delete err.stack; + + new Promise( ( resolve, reject ) => + { + throw err; + } ); + + await triggerUnhandledWarnings( ); + + const linesWoColumns = splitLines( console.error.mock.calls[ 0 ] ) + .filter( line => line.includes( "register.spec.js" ) ) + .map( line => cutColumn( line ) ); + + const errorAndShared = splitLines( console.error.mock.calls[ 0 ] ) + .filter( line => line ); + + expect( linesWoColumns.length ).toBeGreaterThanOrEqual( 2 ); + expect( linesWoColumns[ 0 ] ).not.toBe( linesWoColumns[ 1 ] ); + + expect( errorAndShared[ errorAndShared.length - 2 ] ).toBe( + " ==== Error at: ====================" + ); + expect( errorAndShared[ errorAndShared.length - 1 ] ).toBe( + " ==== Shared trace: ================" + ); + } ) ); + + it( "Handle null error", withConsoleSpy( async ( ) => + { + const err = null; + + new Promise( ( resolve, reject ) => + { + throw err; + } ); + + await triggerUnhandledWarnings( ); + + const linesWoColumns = splitLines( console.error.mock.calls[ 0 ] ) + .filter( line => line.includes( "register.spec.js" ) ) + .map( line => cutColumn( line ) ); + + const errorAndShared = splitLines( console.error.mock.calls[ 0 ] ) + .filter( line => line ); + + expect( linesWoColumns.length ).toBeGreaterThanOrEqual( 2 ); + expect( linesWoColumns[ 0 ] ).not.toBe( linesWoColumns[ 1 ] ); + + expect( errorAndShared[ errorAndShared.length - 2 ] ).toBe( + " ==== Error at: ====================" + ); + expect( errorAndShared[ errorAndShared.length - 1 ] ).toBe( + " ==== Shared trace: ================" + ); + } ) ); } );