@@ -5237,7 +5237,8 @@ describe('utils', () => {
52375237
52385238 sinon . assert . calledOnce ( readPackageStub ) ;
52395239 sinon . assert . calledOnce ( filterStub ) ;
5240- expect ( runSettings . npm_dependencies ) . to . deep . equal ( { } ) ;
5240+ // Should now have browserstack-cypress-cli added automatically
5241+ expect ( runSettings . npm_dependencies ) . to . have . property ( 'browserstack-cypress-cli' ) ;
52415242 } ) ;
52425243
52435244 it ( 'should merge auto-imported deps with existing empty npm_dependencies' , ( ) => {
@@ -5396,7 +5397,7 @@ describe('utils', () => {
53965397 const endTime = Date . now ( ) ;
53975398
53985399 expect ( endTime - startTime ) . to . be . lessThan ( 1000 ) ; // Should complete within 1 second
5399- expect ( Object . keys ( runSettings . npm_dependencies ) ) . to . have . lengthOf ( 500 ) ;
5400+ expect ( Object . keys ( runSettings . npm_dependencies ) ) . to . have . lengthOf ( 501 ) ; // 500 + browserstack-cypress-cli
54005401 } ) ;
54015402
54025403 it ( 'should initialize npm_dependencies when it does not exist' , ( ) => {
@@ -5416,6 +5417,180 @@ describe('utils', () => {
54165417 expect ( runSettings ) . to . have . property ( 'npm_dependencies' ) ;
54175418 expect ( runSettings . npm_dependencies ) . to . deep . equal ( mockDevDeps ) ;
54185419 } ) ;
5420+
5421+ it ( 'should call ensureBrowserstackCypressCliDependency when auto import is enabled' , ( ) => {
5422+ const runSettings = {
5423+ auto_import_dev_dependencies : true ,
5424+ home_directory : '/project/dir'
5425+ } ;
5426+
5427+ const mockDevDeps = { jest : '^29.0.0' } ;
5428+ const ensureStub = sinon . stub ( utils , 'ensureBrowserstackCypressCliDependency' ) ;
5429+
5430+ validateStub . returns ( ) ;
5431+ readPackageStub . returns ( mockDevDeps ) ;
5432+ filterStub . returns ( mockDevDeps ) ;
5433+
5434+ utils . processAutoImportDependencies ( runSettings ) ;
5435+
5436+ sinon . assert . calledOnceWithExactly ( ensureStub , mockDevDeps ) ;
5437+ ensureStub . restore ( ) ;
5438+ } ) ;
5439+
5440+ it ( 'should not call ensureBrowserstackCypressCliDependency when auto import is disabled' , ( ) => {
5441+ const runSettings = {
5442+ auto_import_dev_dependencies : false
5443+ } ;
5444+
5445+ const ensureStub = sinon . stub ( utils , 'ensureBrowserstackCypressCliDependency' ) ;
5446+
5447+ utils . processAutoImportDependencies ( runSettings ) ;
5448+
5449+ sinon . assert . notCalled ( ensureStub ) ;
5450+ ensureStub . restore ( ) ;
5451+ } ) ;
5452+ } ) ;
5453+
5454+ describe ( '#ensureBrowserstackCypressCliDependency' , ( ) => {
5455+ let loggerWarnStub , loggerDebugStub , fsExistsSyncStub ;
5456+
5457+ beforeEach ( ( ) => {
5458+ loggerWarnStub = sinon . stub ( logger , 'warn' ) ;
5459+ loggerDebugStub = sinon . stub ( logger , 'debug' ) ;
5460+ fsExistsSyncStub = sinon . stub ( fs , 'existsSync' ) ;
5461+ } ) ;
5462+
5463+ afterEach ( ( ) => {
5464+ loggerWarnStub . restore ( ) ;
5465+ loggerDebugStub . restore ( ) ;
5466+ fsExistsSyncStub . restore ( ) ;
5467+ // Clear require cache
5468+ delete require . cache [ require . resolve ( '../../../../package.json' ) ] ;
5469+ } ) ;
5470+
5471+ it ( 'should add browserstack-cypress-cli when not present with version from package.json' , ( ) => {
5472+ const npmDependencies = {
5473+ 'cypress' : '^12.0.0' ,
5474+ 'jest' : '^29.0.0'
5475+ } ;
5476+
5477+ fsExistsSyncStub . returns ( true ) ;
5478+
5479+ // Mock require to return a version
5480+ const mockPackageJson = { version : '1.2.3' } ;
5481+
5482+ // Temporarily replace require
5483+ const Module = require ( 'module' ) ;
5484+ const originalLoad = Module . _load ;
5485+ Module . _load = function ( request , parent ) {
5486+ if ( request . includes ( 'package.json' ) ) {
5487+ return mockPackageJson ;
5488+ }
5489+ return originalLoad . call ( this , request , parent ) ;
5490+ } ;
5491+
5492+ utils . ensureBrowserstackCypressCliDependency ( npmDependencies ) ;
5493+
5494+ expect ( npmDependencies ) . to . have . property ( 'browserstack-cypress-cli' , '1.2.3' ) ;
5495+ sinon . assert . calledWith ( loggerWarnStub , 'Missing browserstack-cypress-cli not found in npm_dependencies' ) ;
5496+ sinon . assert . calledWith ( loggerWarnStub , 'Adding browserstack-cypress-cli version 1.2.3 in npm_dependencies' ) ;
5497+
5498+ // Restore require
5499+ Module . _load = originalLoad ;
5500+ } ) ;
5501+
5502+ it ( 'should add browserstack-cypress-cli with "latest" when package.json does not exist' , ( ) => {
5503+ const npmDependencies = {
5504+ 'cypress' : '^12.0.0'
5505+ } ;
5506+
5507+ fsExistsSyncStub . returns ( false ) ;
5508+
5509+ utils . ensureBrowserstackCypressCliDependency ( npmDependencies ) ;
5510+
5511+ expect ( npmDependencies ) . to . have . property ( 'browserstack-cypress-cli' , 'latest' ) ;
5512+ sinon . assert . calledWith ( loggerWarnStub , 'Missing browserstack-cypress-cli not found in npm_dependencies' ) ;
5513+ sinon . assert . calledWith ( loggerWarnStub , 'Adding browserstack-cypress-cli version latest in npm_dependencies' ) ;
5514+ } ) ;
5515+
5516+ it ( 'should add browserstack-cypress-cli with "latest" when require throws error' , ( ) => {
5517+ const npmDependencies = {
5518+ 'cypress' : '^12.0.0'
5519+ } ;
5520+
5521+ fsExistsSyncStub . returns ( true ) ;
5522+
5523+ // Mock require to throw an error
5524+ const Module = require ( 'module' ) ;
5525+ const originalLoad = Module . _load ;
5526+ Module . _load = function ( request , parent ) {
5527+ if ( request . includes ( 'package.json' ) ) {
5528+ throw new Error ( 'Cannot read file' ) ;
5529+ }
5530+ return originalLoad . call ( this , request , parent ) ;
5531+ } ;
5532+
5533+ utils . ensureBrowserstackCypressCliDependency ( npmDependencies ) ;
5534+
5535+ expect ( npmDependencies ) . to . have . property ( 'browserstack-cypress-cli' , 'latest' ) ;
5536+ sinon . assert . calledWith ( loggerDebugStub , "Could not read package.json version, using 'latest'" ) ;
5537+ sinon . assert . calledWith ( loggerWarnStub , 'Adding browserstack-cypress-cli version latest in npm_dependencies' ) ;
5538+
5539+ // Restore require
5540+ Module . _load = originalLoad ;
5541+ } ) ;
5542+
5543+ it ( 'should not modify npmDependencies when browserstack-cypress-cli already exists' , ( ) => {
5544+ const npmDependencies = {
5545+ 'browserstack-cypress-cli' : '^2.5.0' ,
5546+ 'cypress' : '^12.0.0'
5547+ } ;
5548+
5549+ utils . ensureBrowserstackCypressCliDependency ( npmDependencies ) ;
5550+
5551+ expect ( npmDependencies [ 'browserstack-cypress-cli' ] ) . to . equal ( '^2.5.0' ) ;
5552+ sinon . assert . notCalled ( loggerWarnStub ) ;
5553+ sinon . assert . notCalled ( fsExistsSyncStub ) ;
5554+ } ) ;
5555+
5556+ it ( 'should handle undefined npmDependencies parameter' , ( ) => {
5557+ utils . ensureBrowserstackCypressCliDependency ( undefined ) ;
5558+
5559+ sinon . assert . notCalled ( loggerWarnStub ) ;
5560+ sinon . assert . notCalled ( fsExistsSyncStub ) ;
5561+ } ) ;
5562+
5563+ it ( 'should handle null npmDependencies parameter' , ( ) => {
5564+ utils . ensureBrowserstackCypressCliDependency ( null ) ;
5565+
5566+ sinon . assert . notCalled ( loggerWarnStub ) ;
5567+ sinon . assert . notCalled ( fsExistsSyncStub ) ;
5568+ } ) ;
5569+
5570+ it ( 'should handle non-object npmDependencies parameter' , ( ) => {
5571+ utils . ensureBrowserstackCypressCliDependency ( 'not an object' ) ;
5572+
5573+ sinon . assert . notCalled ( loggerWarnStub ) ;
5574+ sinon . assert . notCalled ( fsExistsSyncStub ) ;
5575+ } ) ;
5576+
5577+ it ( 'should handle array npmDependencies parameter' , ( ) => {
5578+ utils . ensureBrowserstackCypressCliDependency ( [ 'not' , 'an' , 'object' ] ) ;
5579+
5580+ sinon . assert . notCalled ( loggerWarnStub ) ;
5581+ sinon . assert . notCalled ( fsExistsSyncStub ) ;
5582+ } ) ;
5583+
5584+ it ( 'should handle empty npmDependencies object' , ( ) => {
5585+ const npmDependencies = { } ;
5586+
5587+ fsExistsSyncStub . returns ( false ) ;
5588+
5589+ utils . ensureBrowserstackCypressCliDependency ( npmDependencies ) ;
5590+
5591+ expect ( npmDependencies ) . to . have . property ( 'browserstack-cypress-cli' , 'latest' ) ;
5592+ sinon . assert . calledWith ( loggerWarnStub , 'Missing browserstack-cypress-cli not found in npm_dependencies' ) ;
5593+ } ) ;
54195594 } ) ;
54205595
54215596} ) ;
0 commit comments