Skip to content

Commit 56b3e23

Browse files
author
Karan Nagpal
committed
Update unit tests
1 parent b9dbae6 commit 56b3e23

File tree

3 files changed

+83
-96
lines changed

3 files changed

+83
-96
lines changed

bin/helpers/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ exports.setLocalMode = (bsConfig, args) => {
632632
exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
633633
return new Promise(async (resolve, reject) => {
634634
if( bsConfig['connection_settings'] && bsConfig['connection_settings']['local'] && String(bsConfig['connection_settings']['local']) === "true" ){
635-
let localBinaryRunning = await this.checklocalBinaryRunning(bsConfig, bsConfig['connection_settings']['local_identifier']);
635+
let localBinaryRunning = await this.checkLocalBinaryRunning(bsConfig, bsConfig['connection_settings']['local_identifier']);
636636
let localIdentifierRunning;
637637
if (localBinaryRunning['should_spawn_binary'] == true) {
638638
localIdentifierRunning = false;
@@ -731,7 +731,7 @@ exports.generateLocalIdentifier = (mode) => {
731731
return Buffer.from(local_identifier).toString("base64");
732732
};
733733

734-
exports.checklocalBinaryRunning = (bsConfig, localIdentifier) => {
734+
exports.checkLocalBinaryRunning = (bsConfig, localIdentifier) => {
735735
let options = {
736736
url: `${config.cypress_v1}/local_binary_running_check`,
737737
auth: {

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,37 +162,47 @@ describe("capabilityHelper.js", () => {
162162
});
163163
});
164164

165-
it("handle local_identifier set", () => {
166-
let zip_url = "bs://<random>";
167-
let bsConfig = {
168-
auth: {
169-
username: "random",
170-
access_key: "random",
171-
},
172-
browsers: [
173-
{
174-
browser: "chrome",
175-
os: "Windows 10",
176-
versions: ["78", "77"],
165+
context("handle local_identifier set when local binary spawned", () => {
166+
beforeEach(() => {
167+
process.env.BSTACK_CYPRESS_RUN_LOCAL_BINARY = "true";
168+
});
169+
170+
afterEach(() => {
171+
delete process.env.BSTACK_CYPRESS_RUN_LOCAL_BINARY;
172+
});
173+
174+
it("handle local_identifier set", () => {
175+
let zip_url = "bs://<random>";
176+
let bsConfig = {
177+
auth: {
178+
username: "random",
179+
access_key: "random",
177180
},
178-
],
179-
connection_settings: {
180-
local: true,
181-
local_identifier: "abc"
182-
},
183-
run_settings: {
184-
}
185-
};
186-
return capabilityHelper
187-
.caps(bsConfig, { zip_url: zip_url })
188-
.then(function (data) {
189-
let parsed_data = JSON.parse(data);
190-
chai.assert.equal(parsed_data.local, true);
191-
chai.assert.equal(parsed_data.localIdentifier, "abc");
192-
})
193-
.catch((error) => {
194-
chai.assert.fail("Promise error");
195-
});
181+
browsers: [
182+
{
183+
browser: "chrome",
184+
os: "Windows 10",
185+
versions: ["78", "77"],
186+
},
187+
],
188+
connection_settings: {
189+
local: true,
190+
local_identifier: "abc"
191+
},
192+
run_settings: {
193+
}
194+
};
195+
return capabilityHelper
196+
.caps(bsConfig, { zip_url: zip_url })
197+
.then(function (data) {
198+
let parsed_data = JSON.parse(data);
199+
chai.assert.equal(parsed_data.local, true);
200+
chai.assert.equal(parsed_data.localIdentifier, "abc");
201+
})
202+
.catch((error) => {
203+
chai.assert.fail("Promise error");
204+
});
205+
});
196206
});
197207

198208
it("handle local_identifier not set", () => {

test/unit/bin/helpers/utils.js

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ describe('utils', () => {
12341234
sandbox.restore();
12351235
});
12361236

1237-
it('if local is true and localIdentifier is not running and start error is raised', () => {
1237+
it('if local is true and localBinary is not running and start error is raised', () => {
12381238
let bsConfig = {
12391239
auth: {
12401240
access_key: 'xyz',
@@ -1245,11 +1245,11 @@ describe('utils', () => {
12451245
},
12461246
};
12471247
let args = {};
1248-
let checkLocalIdentifierRunningStub = sinon.stub(
1248+
let checkLocalBinaryRunningStub = sinon.stub(
12491249
utils,
1250-
'checkLocalIdentifierRunning'
1250+
'checkLocalBinaryRunning'
12511251
);
1252-
checkLocalIdentifierRunningStub.returns(Promise.resolve(false));
1252+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": true}));
12531253
let setLocalArgsStub = sinon.stub(utils, 'setLocalArgs');
12541254
setLocalArgsStub.returns({});
12551255
let localBinaryStartStub = sandbox
@@ -1270,7 +1270,7 @@ describe('utils', () => {
12701270
});
12711271
});
12721272

1273-
it('if local is true and localIdentifier is not running and start error is not raised', () => {
1273+
it('if local is true and localBinary is not running and start error is not raised', () => {
12741274
let bsConfig = {
12751275
auth: {
12761276
access_key: 'xyz',
@@ -1286,11 +1286,11 @@ describe('utils', () => {
12861286
localIdentifier: 'abc',
12871287
daemon: true,
12881288
};
1289-
let checkLocalIdentifierRunningStub = sinon.stub(
1289+
let checkLocalBinaryRunningStub = sinon.stub(
12901290
utils,
1291-
'checkLocalIdentifierRunning'
1291+
'checkLocalBinaryRunning'
12921292
);
1293-
checkLocalIdentifierRunningStub.returns(Promise.resolve(false));
1293+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": true}));
12941294
let setLocalArgsStub = sinon.stub(utils, 'setLocalArgs');
12951295
setLocalArgsStub.returns(localArgs);
12961296

@@ -1322,11 +1322,11 @@ describe('utils', () => {
13221322
},
13231323
};
13241324
let args = {};
1325-
let checkLocalIdentifierRunningStub = sinon.stub(
1325+
let checkLocalBinaryRunningStub = sinon.stub(
13261326
utils,
1327-
'checkLocalIdentifierRunning'
1327+
'checkLocalBinaryRunning'
13281328
);
1329-
checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1329+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": false}));
13301330
return utils.setupLocalTesting(bsConfig, args).then((result) => {
13311331
expect(result).to.be.eq(undefined);
13321332
});
@@ -1373,19 +1373,19 @@ describe('utils', () => {
13731373
local: true,
13741374
},
13751375
};
1376-
let checkLocalIdentifierRunningStub = sinon.stub(
1376+
let checkLocalBinaryRunningStub = sinon.stub(
13771377
utils,
1378-
'checkLocalIdentifierRunning'
1378+
'checkLocalBinaryRunning'
13791379
);
1380-
checkLocalIdentifierRunningStub.returns(Promise.resolve(false));
1380+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": true}));
13811381
let sendUsageReportStub = sandbox
13821382
.stub(utils, 'sendUsageReport')
13831383
.callsFake(function () {
13841384
return 'end';
13851385
});
1386-
return utils.stopLocalBinary(bsConfig).then((result) => {
1386+
return utils.stopLocalBinary(bsConfig, null, null, null).then((result) => {
13871387
expect(result).to.be.eq(undefined);
1388-
sinon.assert.calledOnce(sendUsageReportStub);
1388+
sinon.assert.notCalled(sendUsageReportStub);
13891389
});
13901390
});
13911391

@@ -1399,11 +1399,11 @@ describe('utils', () => {
13991399
let bs_local = {
14001400
isRunning: isRunningStub,
14011401
};
1402-
let checkLocalIdentifierRunningStub = sinon.stub(
1402+
let checkLocalBinaryRunningStub = sinon.stub(
14031403
utils,
1404-
'checkLocalIdentifierRunning'
1404+
'checkLocalBinaryRunning'
14051405
);
1406-
checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1406+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": false}));
14071407
return utils.stopLocalBinary(bsConfig, bs_local).then((result) => {
14081408
expect(result).to.be.eq(undefined);
14091409
});
@@ -1419,11 +1419,11 @@ describe('utils', () => {
14191419
let bs_local = {
14201420
isRunning: isRunningStub,
14211421
};
1422-
let checkLocalIdentifierRunningStub = sinon.stub(
1422+
let checkLocalBinaryRunningStub = sinon.stub(
14231423
utils,
1424-
'checkLocalIdentifierRunning'
1424+
'checkLocalBinaryRunning'
14251425
);
1426-
checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1426+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": false}));
14271427
return utils.stopLocalBinary(bsConfig, bs_local).then((result) => {
14281428
expect(result).to.be.eq(undefined);
14291429
});
@@ -1441,11 +1441,11 @@ describe('utils', () => {
14411441
isRunning: isRunningStub,
14421442
stop: stopStub,
14431443
};
1444-
let checkLocalIdentifierRunningStub = sinon.stub(
1444+
let checkLocalBinaryRunningStub = sinon.stub(
14451445
utils,
1446-
'checkLocalIdentifierRunning'
1446+
'checkLocalBinaryRunning'
14471447
);
1448-
checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1448+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": false}));
14491449
return utils.stopLocalBinary(bsConfig, bs_local).then((result) => {
14501450
expect(result).to.be.eq(undefined);
14511451
});
@@ -1460,11 +1460,11 @@ describe('utils', () => {
14601460
let isRunningStub = sandbox.stub().returns(true);
14611461
let error = new Error('Local Stop Error');
14621462
let stopStub = sandbox.stub().yields(error);
1463-
let checkLocalIdentifierRunningStub = sinon.stub(
1463+
let checkLocalBinaryRunningStub = sinon.stub(
14641464
utils,
1465-
'checkLocalIdentifierRunning'
1465+
'checkLocalBinaryRunning'
14661466
);
1467-
checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1467+
checkLocalBinaryRunningStub.returns(Promise.resolve({"should_spawn_binary": false}));
14681468
let bs_local = {
14691469
isRunning: isRunningStub,
14701470
stop: stopStub,
@@ -1503,7 +1503,7 @@ describe('utils', () => {
15031503
afterEach(function () {
15041504
delete process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
15051505
});
1506-
it('should generate local_identifier if args.localIdentifier & process.env.BROWSERSTACK_LOCAL_IDENTIFIER is undefined', () => {
1506+
it('should not generate local_identifier if args.localIdentifier & process.env.BROWSERSTACK_LOCAL_IDENTIFIER is undefined', () => {
15071507
let bsConfig = {
15081508
connection_settings: {
15091509
local: true,
@@ -1516,7 +1516,8 @@ describe('utils', () => {
15161516
);
15171517
generateLocalIdentifierStub.returns('abc');
15181518
utils.setLocalIdentifier(bsConfig, args);
1519-
expect(bsConfig.connection_settings.local_identifier).to.be.eq('abc');
1519+
sinon.assert.notCalled(generateLocalIdentifierStub);
1520+
expect(bsConfig.connection_settings.local_identifier).to.be.eq(undefined);
15201521
});
15211522

15221523
it('should change local identifier to local_identifier in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is set to local_identifier', () => {
@@ -2392,7 +2393,7 @@ describe('utils', () => {
23922393
});
23932394
});
23942395

2395-
describe('#checkLocalIdentifierRunning', () => {
2396+
describe('#checkLocalBinaryRunning', () => {
23962397
afterEach(() => {
23972398
sinon.restore();
23982399
});
@@ -2404,21 +2405,10 @@ describe('utils', () => {
24042405
},
24052406
};
24062407
const responseBody = {
2407-
status: 'success',
2408-
instances: [
2409-
{
2410-
localIdentifier: 'abcdef',
2411-
},
2412-
{
2413-
localIdentifier: 'ghij',
2414-
},
2415-
{
2416-
localIdentifier: 'lmno',
2417-
},
2418-
],
2408+
"should_spawn_binary": true
24192409
};
24202410
sinon
2421-
.stub(request, 'get')
2411+
.stub(request, 'post')
24222412
.yields(undefined, responseObject, JSON.stringify(responseBody));
24232413

24242414
let bsConfig = {
@@ -2430,35 +2420,22 @@ describe('utils', () => {
24302420

24312421
let localIdentifier = 'abcd';
24322422
return utils
2433-
.checkLocalIdentifierRunning(bsConfig, localIdentifier)
2423+
.checkLocalBinaryRunning(bsConfig, localIdentifier)
24342424
.then((result) => {
2435-
expect(result).to.be.eq(false);
2425+
chai.assert.deepEqual(result, {"should_spawn_binary": true});
24362426
});
24372427
});
24382428

2439-
it('if the bsConfig localIdentifier if present within the response body then the function should resolve with true', () => {
2429+
it('if the bsConfig localIdentifier is present within the response body', () => {
24402430
const responseObject = {
24412431
statusCode: 200,
24422432
headers: {
24432433
'content-type': 'application/json',
24442434
},
24452435
};
2446-
const responseBody = {
2447-
status: 'success',
2448-
instances: [
2449-
{
2450-
localIdentifier: 'abcdef',
2451-
},
2452-
{
2453-
localIdentifier: 'ghij',
2454-
},
2455-
{
2456-
localIdentifier: 'lmno',
2457-
},
2458-
],
2459-
};
2436+
const responseBody = { "should_spawn_binary": false };
24602437
sinon
2461-
.stub(request, 'get')
2438+
.stub(request, 'post')
24622439
.yields(undefined, responseObject, JSON.stringify(responseBody));
24632440

24642441
let bsConfig = {
@@ -2470,9 +2447,9 @@ describe('utils', () => {
24702447

24712448
let localIdentifier = 'lmno';
24722449
return utils
2473-
.checkLocalIdentifierRunning(bsConfig, localIdentifier)
2450+
.checkLocalBinaryRunning(bsConfig, localIdentifier)
24742451
.then((result) => {
2475-
expect(result).to.be.eq(true);
2452+
chai.assert.deepEqual(result, {"should_spawn_binary": false})
24762453
});
24772454
});
24782455
});

0 commit comments

Comments
 (0)