1+ import * as fs from 'fs' ;
12import * as _ from 'lodash' ;
3+ import { join } from 'path' ;
4+ import * as proxyquire from 'proxyquire' ;
25import * as sinon from 'sinon' ;
36import { ChromeDebugAdapter } from 'vscode-chrome-debug-core' ;
47import { Event } from 'vscode-debugadapter' ;
58import * as extProtocol from '../common/extensionProtocol' ;
6- import { NativeScriptDebugAdapter } from '../debug-adapter/nativeScriptDebugAdapter' ;
9+ const appRoot = 'appRootMock' ;
10+ const webpackConfigFunctionStub = sinon . stub ( ) ;
11+
12+ proxyquire . noCallThru ( ) ;
13+ const nativeScriptDebugAdapterLib = proxyquire ( '../debug-adapter/nativeScriptDebugAdapter' , {
14+ [ join ( appRoot , 'webpack.config.js' ) ] : webpackConfigFunctionStub ,
15+ } ) ;
716
817const examplePort = 456 ;
918
@@ -17,7 +26,7 @@ const customMessagesResponses = {
1726} ;
1827
1928const defaultArgsMock : any = {
20- appRoot : 'appRootMock' ,
29+ appRoot,
2130 diagnosticLogging : true ,
2231 platform : 'android' ,
2332 request : 'attach' ,
@@ -67,7 +76,7 @@ describe('NativeScriptDebugAdapter', () => {
6776 setTransformOptions : ( ) => undefined ,
6877 } ;
6978
70- nativeScriptDebugAdapter = new NativeScriptDebugAdapter ( {
79+ nativeScriptDebugAdapter = new nativeScriptDebugAdapterLib . NativeScriptDebugAdapter ( {
7180 chromeConnection : mockConstructor ( chromeConnectionMock ) ,
7281 pathTransformer : mockConstructor ( pathTransformerMock ) ,
7382 sourceMapTransformer : mockConstructor ( sourceMapTransformer ) ,
@@ -83,7 +92,11 @@ describe('NativeScriptDebugAdapter', () => {
8392
8493 platforms . forEach ( ( platform ) => {
8594 launchMethods . forEach ( ( method ) => {
86- const argsMock = _ . merge ( { } , defaultArgsMock , { platform, request : method } ) ;
95+ let argsMock : any ;
96+
97+ beforeEach ( ( ) => {
98+ argsMock = _ . merge ( { } , defaultArgsMock , { platform, request : method } ) ;
99+ } ) ;
87100
88101 it ( `${ method } for ${ platform } should raise debug start event` , async ( ) => {
89102 const spy = sinon . spy ( chromeSessionMock , 'sendEvent' ) ;
@@ -121,7 +134,51 @@ describe('NativeScriptDebugAdapter', () => {
121134
122135 sinon . assert . calledWith ( spy , sinon . match ( {
123136 trace : true ,
124- webRoot : 'appRootMock' ,
137+ webRoot : appRoot ,
138+ } ) ) ;
139+ } ) ;
140+
141+ it ( `${ method } for ${ platform } should add sourceMapPathOverrides data` , async ( ) => {
142+ const spy = sinon . spy ( ChromeDebugAdapter . prototype , 'attach' ) ;
143+ const existsSyncStub = sinon . stub ( fs , 'existsSync' ) ;
144+
145+ existsSyncStub . returns ( true ) ;
146+ webpackConfigFunctionStub
147+ . withArgs ( { [ platform ] : platform } )
148+ . returns ( { output : { library : 'myLib' } } ) ;
149+
150+ await nativeScriptDebugAdapter [ method ] ( argsMock ) ;
151+
152+ existsSyncStub . restore ( ) ;
153+ sinon . assert . calledWith ( spy , sinon . match ( {
154+ sourceMapPathOverrides : {
155+ 'webpack:///*' : `${ join ( appRoot , 'app' ) } /*` ,
156+ 'webpack://myLib/*' : `${ join ( appRoot , 'app' ) } /*` ,
157+ } ,
158+ trace : true ,
159+ webRoot : appRoot ,
160+ } ) ) ;
161+
162+ } ) ;
163+
164+ it ( `${ method } for ${ platform } should not fail when unable to require webpack.config.js` , async ( ) => {
165+ const spy = sinon . spy ( ChromeDebugAdapter . prototype , 'attach' ) ;
166+ const existsSyncStub = sinon . stub ( fs , 'existsSync' ) ;
167+
168+ existsSyncStub . returns ( true ) ;
169+ webpackConfigFunctionStub
170+ . withArgs ( { [ platform ] : platform } )
171+ . throws ( new Error ( 'test' ) ) ;
172+
173+ await nativeScriptDebugAdapter [ method ] ( argsMock ) ;
174+
175+ existsSyncStub . restore ( ) ;
176+ sinon . assert . calledWith ( spy , sinon . match ( {
177+ sourceMapPathOverrides : {
178+ 'webpack:///*' : `${ join ( appRoot , 'app' ) } /*` ,
179+ } ,
180+ trace : true ,
181+ webRoot : appRoot ,
125182 } ) ) ;
126183 } ) ;
127184
0 commit comments