CLIUtils.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../../../src/types/options/TInputOptions';
  3. import { StringArrayEncoding } from '../../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
  4. import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
  5. describe('CLIUtils', () => {
  6. describe('getUserConfig', () => {
  7. describe('Variant #1: valid config file path', () => {
  8. describe('Variant #1: js file with config', () => {
  9. const configDirName: string = 'test/fixtures';
  10. const configFileName: string = 'config.js';
  11. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  12. const expectedResult: TInputOptions = {
  13. compact: true,
  14. exclude: ['**/foo.js'],
  15. selfDefending: false,
  16. sourceMap: true
  17. };
  18. let result: Object;
  19. before(() => {
  20. result = CLIUtils.getUserConfig(configFilePath);
  21. });
  22. it('should return object with user configuration', () => {
  23. assert.deepEqual(result, expectedResult);
  24. });
  25. });
  26. describe('Variant #2: cjs file with config', () => {
  27. const configDirName: string = 'test/fixtures';
  28. const configFileName: string = 'config.cjs';
  29. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  30. const expectedResult: TInputOptions = {
  31. compact: true,
  32. exclude: ['**/foo.js'],
  33. selfDefending: false,
  34. sourceMap: true
  35. };
  36. let result: Object;
  37. before(() => {
  38. result = CLIUtils.getUserConfig(configFilePath);
  39. });
  40. it('should return object with user configuration', () => {
  41. assert.deepEqual(result, expectedResult);
  42. });
  43. });
  44. describe('Variant #3: json file with config', () => {
  45. const configDirName: string = 'test/fixtures';
  46. const configFileName: string = 'config.json';
  47. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  48. const expectedResult: TInputOptions = {
  49. compact: true,
  50. selfDefending: false,
  51. sourceMap: true
  52. };
  53. let result: Object;
  54. before(() => {
  55. result = CLIUtils.getUserConfig(configFilePath);
  56. });
  57. it('should return object with user configuration', () => {
  58. assert.deepEqual(result, expectedResult);
  59. });
  60. });
  61. });
  62. describe('Variant #2: invalid config file extension', () => {
  63. const configDirName: string = 'test/fixtures';
  64. const configFileName: string = 'configs.config';
  65. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  66. let testFunc: () => void;
  67. before(() => {
  68. testFunc = () => CLIUtils.getUserConfig(configFilePath);
  69. });
  70. it('should throw an error if `configFilePath` is not a valid path', () => {
  71. assert.throws(testFunc, /Given config path must be a valid/);
  72. });
  73. });
  74. describe('Variant #3: invalid config file path', () => {
  75. const configDirName: string = 'test/fixtures';
  76. const configFileName: string = 'configs.js';
  77. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  78. let testFunc: () => void;
  79. before(() => {
  80. testFunc = () => CLIUtils.getUserConfig(configFilePath);
  81. });
  82. it('should throw an error if `configFilePath` is not a valid path', () => {
  83. assert.throws(testFunc, /Cannot open config file/);
  84. });
  85. });
  86. });
  87. describe('stringifyOptionAvailableValues', () => {
  88. describe('Variant #1: should stringify option available values', () => {
  89. const expectedResult: string = 'none, base64, rc4';
  90. let result: Object;
  91. before(() => {
  92. result = CLIUtils.stringifyOptionAvailableValues(StringArrayEncoding);
  93. });
  94. it('should return option available values as string', () => {
  95. assert.deepEqual(result, expectedResult);
  96. });
  97. });
  98. });
  99. });