CLIUtils.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../../../src/types/options/TInputOptions';
  3. import { StringArrayEncoding } from '../../../../src/enums/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: json file with config', () => {
  27. const configDirName: string = 'test/fixtures';
  28. const configFileName: string = 'config.json';
  29. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  30. const expectedResult: TInputOptions = {
  31. compact: true,
  32. selfDefending: false,
  33. sourceMap: true
  34. };
  35. let result: Object;
  36. before(() => {
  37. result = CLIUtils.getUserConfig(configFilePath);
  38. });
  39. it('should return object with user configuration', () => {
  40. assert.deepEqual(result, expectedResult);
  41. });
  42. });
  43. });
  44. describe('Variant #2: invalid config file extension', () => {
  45. const configDirName: string = 'test/fixtures';
  46. const configFileName: string = 'configs.config';
  47. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  48. let testFunc: () => void;
  49. before(() => {
  50. testFunc = () => CLIUtils.getUserConfig(configFilePath);
  51. });
  52. it('should throw an error if `configFilePath` is not a valid path', () => {
  53. assert.throws(testFunc, /Given config path must be a valid/);
  54. });
  55. });
  56. describe('Variant #3: invalid config file path', () => {
  57. const configDirName: string = 'test/fixtures';
  58. const configFileName: string = 'configs.js';
  59. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  60. let testFunc: () => void;
  61. before(() => {
  62. testFunc = () => CLIUtils.getUserConfig(configFilePath);
  63. });
  64. it('should throw an error if `configFilePath` is not a valid path', () => {
  65. assert.throws(testFunc, /Cannot open config file/);
  66. });
  67. });
  68. });
  69. describe('stringifyOptionAvailableValues', () => {
  70. describe('Variant #1: should stringify option available values', () => {
  71. const expectedResult: string = 'none, base64, rc4';
  72. let result: Object;
  73. before(() => {
  74. result = CLIUtils.stringifyOptionAvailableValues(StringArrayEncoding);
  75. });
  76. it('should return option available values as string', () => {
  77. assert.deepEqual(result, expectedResult);
  78. });
  79. });
  80. });
  81. });