|
| 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