Skip to content

Commit 844c9a9

Browse files
Add UTs
1 parent bcf86c8 commit 844c9a9

File tree

3 files changed

+145
-8
lines changed

3 files changed

+145
-8
lines changed

bin/helpers/readCypressConfigUtil.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
const path = require("path");
33
const fs = require("fs");
4-
const { execSync } = require('child_process');
4+
const cp = require('child_process');
55

66
const config = require('./config');
77
const constants = require("./constants");
@@ -16,15 +16,14 @@ exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_module
1616
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
1717
const working_dir = path.dirname(cypress_config_filepath);
1818
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
19-
execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
20-
execSync(`mkdir ${config.compiledConfigJsDirName}`, { cwd: working_dir })
19+
cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
20+
cp.execSync(`mkdir ${config.compiledConfigJsDirName}`, { cwd: working_dir })
2121

2222
let tsc_command = `NODE_PATH=${bstack_node_modules_path} ${bstack_node_modules_path}/typescript/bin/tsc --outDir ${complied_js_dir} --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false ${cypress_config_filepath}`
2323
let tsc_output
24-
2524
try {
2625
logger.debug(`Running: ${tsc_command}`)
27-
tsc_output = execSync(tsc_command, { cwd: working_dir })
26+
tsc_output = cp.execSync(tsc_command, { cwd: working_dir })
2827
} catch (err) {
2928
// error while compiling ts files
3029
logger.debug(err.message);
@@ -55,7 +54,7 @@ exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_module
5554

5655
exports.loadJsFile = (cypress_config_filepath, bstack_node_modules_path) => {
5756
const require_module_helper_path = `${__dirname}/requireModule.js`
58-
execSync(`NODE_PATH=${bstack_node_modules_path} node ${require_module_helper_path} ${cypress_config_filepath}`)
57+
cp.execSync(`NODE_PATH=${bstack_node_modules_path} node ${require_module_helper_path} ${cypress_config_filepath}`)
5958
const cypress_config = JSON.parse(fs.readFileSync(config.configJsonFileName).toString())
6059
if (fs.existsSync(config.configJsonFileName)) {
6160
fs.unlinkSync(config.configJsonFileName)
@@ -84,6 +83,6 @@ exports.readCypressConfigFile = (bsConfig) => {
8483
// TODO: Add instrumention if error occurred
8584
} finally {
8685
const working_dir = path.dirname(cypress_config_filepath);
87-
execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
86+
cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
8887
}
8988
}

test/unit/bin/commands/runs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ describe("runs", () => {
808808
});
809809

810810

811-
describe.only("handle createBuild success", () => {
811+
describe("handle createBuild success", () => {
812812
var sandbox;
813813

814814
beforeEach(() => {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
'use strict';
2+
const chai = require("chai"),
3+
expect = chai.expect,
4+
sinon = require("sinon"),
5+
EventEmitter = require('events');
6+
7+
const logger = require("../../../../bin/helpers/logger").winstonLogger;
8+
9+
const cp = require("child_process");
10+
const fs = require("fs");
11+
const rewire = require("rewire");
12+
const readCypressConfigUtil = require("../../../../bin/helpers/readCypressConfigUtil");
13+
14+
logger.transports["console.info"].silent = true;
15+
16+
17+
describe("readCypressConfigUtil", () => {
18+
let sandbox;
19+
20+
beforeEach(() => {
21+
sandbox = sinon.createSandbox();
22+
});
23+
24+
afterEach(() => {
25+
sandbox.restore();
26+
sinon.restore();
27+
});
28+
29+
describe('detectLanguage', () => {
30+
it('should return file extension', () => {
31+
const result = readCypressConfigUtil.detectLanguage('cypress.config.ts');
32+
expect(result).to.eql('ts');
33+
});
34+
35+
it('should return file js extension if not matched with defined ones', () => {
36+
const result = readCypressConfigUtil.detectLanguage('cypress.config.mts');
37+
expect(result).to.eql('js');
38+
});
39+
});
40+
41+
describe('loadJsFile', () => {
42+
it('should load js file', () => {
43+
sandbox.stub(cp, "execSync").returns("random string");
44+
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
45+
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
46+
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
47+
48+
const result = readCypressConfigUtil.loadJsFile('path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
49+
50+
expect(result).to.eql({ e2e: {} });
51+
sinon.assert.calledOnce(readFileSyncStub);
52+
sinon.assert.calledOnce(unlinkSyncSyncStub);
53+
sinon.assert.calledOnce(existsSyncStub);
54+
});
55+
});
56+
57+
describe('convertTsConfig', () => {
58+
it('should compile cypress.config.ts to cypress.config.js', () => {
59+
const bsConfig = {
60+
run_settings: {
61+
cypressConfigFilePath: 'path/to/cypress.config.ts',
62+
cypress_config_filename: 'cypress.config.ts'
63+
}
64+
};
65+
sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
66+
67+
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
68+
69+
expect(result).to.eql('path/to/compiled/cypress.config.js');
70+
});
71+
72+
it('should return null if compilation fails', () => {
73+
const bsConfig = {
74+
run_settings: {
75+
cypressConfigFilePath: 'path/to/cypress.config.ts',
76+
cypress_config_filename: 'cypress.config.ts'
77+
}
78+
};
79+
sandbox.stub(cp, "execSync").returns("Error: some error\n");
80+
81+
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
82+
83+
expect(result).to.eql(null);
84+
});
85+
86+
it('should compile cypress.config.ts to cypress.config.js if unrelevant error', () => {
87+
const bsConfig = {
88+
run_settings: {
89+
cypressConfigFilePath: 'path/to/folder/cypress.config.ts',
90+
cypress_config_filename: 'cypress.config.ts'
91+
}
92+
};
93+
const execSyncStub = sandbox.stub(cp, "execSync")
94+
execSyncStub
95+
.withArgs(`NODE_PATH=path/to/tmpBstackPackages path/to/tmpBstackPackages/typescript/bin/tsc --outDir path/to/tmpBstackCompiledJs --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false path/to/cypress.config.ts`, { cwd: 'path/to' })
96+
.throws({
97+
output: Buffer.from("Error: Some Error \n TSFILE: path/to/compiled/cypress.config.js")
98+
});
99+
100+
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
101+
102+
expect(result).to.eql('path/to/compiled/cypress.config.js');
103+
});
104+
});
105+
106+
describe('readCypressConfigFile', () => {
107+
it('should read js file', () => {
108+
const bsConfig = {
109+
run_settings: {
110+
cypressConfigFilePath: 'path/to/cypress.config.js',
111+
cypress_config_filename: 'cypress.config.js'
112+
}
113+
};
114+
sandbox.stub(readCypressConfigUtil, 'loadJsFile').returns({e2e: {}});
115+
sandbox.stub(cp, 'execSync');
116+
117+
const result = readCypressConfigUtil.readCypressConfigFile(bsConfig);
118+
119+
expect(result).to.eql({ e2e: {} });
120+
});
121+
122+
it('should read ts file', () => {
123+
const bsConfig = {
124+
run_settings: {
125+
cypressConfigFilePath: 'path/to/cypress.config.ts',
126+
cypress_config_filename: 'cypress.config.ts'
127+
}
128+
};
129+
sandbox.stub(readCypressConfigUtil, 'convertTsConfig').returns('path/to/compiled/cypress.config.js');
130+
sandbox.stub(readCypressConfigUtil, 'loadJsFile').returns({e2e: {}});
131+
sandbox.stub(cp, 'execSync');
132+
133+
const result = readCypressConfigUtil.readCypressConfigFile(bsConfig);
134+
135+
expect(result).to.eql({ e2e: {} });
136+
});
137+
});
138+
});

0 commit comments

Comments
 (0)