@nest-yalc-2/utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +9 -0
  3. package/package.json +29 -0
  4. package/src/__mocks__/enum.mock.ts +9 -0
  5. package/src/__tests__/class.helper.spec.ts +32 -0
  6. package/src/__tests__/command.helper.spec.ts +35 -0
  7. package/src/__tests__/config-manager.helper.spec.ts +93 -0
  8. package/src/__tests__/custom-validator.spec.ts +34 -0
  9. package/src/__tests__/data-structure.helper.spec.ts +30 -0
  10. package/src/__tests__/date.helper.spec.ts +78 -0
  11. package/src/__tests__/encryption.helper.spec.ts +45 -0
  12. package/src/__tests__/enum.helper.spec.ts +53 -0
  13. package/src/__tests__/env.helper.spec.ts +68 -0
  14. package/src/__tests__/error.helper.spec.ts +20 -0
  15. package/src/__tests__/faker-helper.spec.ts +187 -0
  16. package/src/__tests__/files.helper.spec.ts +22 -0
  17. package/src/__tests__/http.helper.spec.ts +15 -0
  18. package/src/__tests__/interval.helper.spec.ts +29 -0
  19. package/src/__tests__/math.helper.spec.ts +24 -0
  20. package/src/__tests__/nest.decorator.spec.ts +29 -0
  21. package/src/__tests__/nest.helper.spec.ts +26 -0
  22. package/src/__tests__/object-mapper.helper.spec.ts +167 -0
  23. package/src/__tests__/object-mapper.interceptor.spec.ts +104 -0
  24. package/src/__tests__/object.helper.spec.ts +181 -0
  25. package/src/__tests__/plugin.helper.spec.ts +85 -0
  26. package/src/__tests__/promise.helper.spec.ts +76 -0
  27. package/src/__tests__/returnValue.spec.ts +49 -0
  28. package/src/__tests__/rxjs.helper.spec.ts +79 -0
  29. package/src/__tests__/simple-mapper.interceptor.spec.ts +82 -0
  30. package/src/__tests__/tsconfig.json +3 -0
  31. package/src/__tests__/validator-helper.spec.ts +34 -0
  32. package/src/__tests__/zlib.helper.spec.ts +20 -0
  33. package/src/class.helper.d.ts +4 -0
  34. package/src/class.helper.js +19 -0
  35. package/src/class.helper.js.map +1 -0
  36. package/src/class.helper.ts +33 -0
  37. package/src/command.helper.d.ts +1 -0
  38. package/src/command.helper.js +16 -0
  39. package/src/command.helper.js.map +1 -0
  40. package/src/command.helper.ts +19 -0
  41. package/src/config-manager.helper.d.ts +12 -0
  42. package/src/config-manager.helper.js +60 -0
  43. package/src/config-manager.helper.js.map +1 -0
  44. package/src/config-manager.helper.ts +122 -0
  45. package/src/custom-validator.d.ts +3 -0
  46. package/src/custom-validator.js +13 -0
  47. package/src/custom-validator.js.map +1 -0
  48. package/src/custom-validator.ts +13 -0
  49. package/src/data-structure.helper.d.ts +1 -0
  50. package/src/data-structure.helper.js +13 -0
  51. package/src/data-structure.helper.js.map +1 -0
  52. package/src/data-structure.helper.ts +14 -0
  53. package/src/date.helper.d.ts +12 -0
  54. package/src/date.helper.js +47 -0
  55. package/src/date.helper.js.map +1 -0
  56. package/src/date.helper.ts +65 -0
  57. package/src/encryption.helper.d.ts +2 -0
  58. package/src/encryption.helper.js +58 -0
  59. package/src/encryption.helper.js.map +1 -0
  60. package/src/encryption.helper.ts +29 -0
  61. package/src/enum.helper.d.ts +8 -0
  62. package/src/enum.helper.js +21 -0
  63. package/src/enum.helper.js.map +1 -0
  64. package/src/enum.helper.ts +34 -0
  65. package/src/env.helper.d.ts +3 -0
  66. package/src/env.helper.js +23 -0
  67. package/src/env.helper.js.map +1 -0
  68. package/src/env.helper.ts +35 -0
  69. package/src/error.helper.d.ts +1 -0
  70. package/src/error.helper.js +12 -0
  71. package/src/error.helper.js.map +1 -0
  72. package/src/error.helper.ts +12 -0
  73. package/src/faker-helper.d.ts +16 -0
  74. package/src/faker-helper.js +73 -0
  75. package/src/faker-helper.js.map +1 -0
  76. package/src/faker-helper.ts +98 -0
  77. package/src/files.helper.d.ts +2 -0
  78. package/src/files.helper.js +13 -0
  79. package/src/files.helper.js.map +1 -0
  80. package/src/files.helper.ts +20 -0
  81. package/src/http.helper.d.ts +26 -0
  82. package/src/http.helper.js +84 -0
  83. package/src/http.helper.js.map +1 -0
  84. package/src/http.helper.ts +152 -0
  85. package/src/index.d.ts +21 -0
  86. package/src/index.js +38 -0
  87. package/src/index.js.map +1 -0
  88. package/src/index.ts +23 -0
  89. package/src/interval.helper.d.ts +5 -0
  90. package/src/interval.helper.js +16 -0
  91. package/src/interval.helper.js.map +1 -0
  92. package/src/interval.helper.ts +13 -0
  93. package/src/math.helper.d.ts +1 -0
  94. package/src/math.helper.js +8 -0
  95. package/src/math.helper.js.map +1 -0
  96. package/src/math.helper.ts +4 -0
  97. package/src/nest.decorator.d.ts +3 -0
  98. package/src/nest.decorator.js +10 -0
  99. package/src/nest.decorator.js.map +1 -0
  100. package/src/nest.decorator.ts +10 -0
  101. package/src/nestjs/nest.decorator.d.ts +3 -0
  102. package/src/nestjs/nest.decorator.js +10 -0
  103. package/src/nestjs/nest.decorator.js.map +1 -0
  104. package/src/nestjs/nest.decorator.ts +10 -0
  105. package/src/nestjs/nest.helper.d.ts +2 -0
  106. package/src/nestjs/nest.helper.js +7 -0
  107. package/src/nestjs/nest.helper.js.map +1 -0
  108. package/src/nestjs/nest.helper.ts +5 -0
  109. package/src/object-mapper.helper.d.ts +16 -0
  110. package/src/object-mapper.helper.js +47 -0
  111. package/src/object-mapper.helper.js.map +1 -0
  112. package/src/object-mapper.helper.ts +148 -0
  113. package/src/object-mapper.interceptor.d.ts +16 -0
  114. package/src/object-mapper.interceptor.js +36 -0
  115. package/src/object-mapper.interceptor.js.map +1 -0
  116. package/src/object-mapper.interceptor.ts +51 -0
  117. package/src/object.helper.d.ts +18 -0
  118. package/src/object.helper.js +91 -0
  119. package/src/object.helper.js.map +1 -0
  120. package/src/object.helper.ts +173 -0
  121. package/src/plugin.helper.d.ts +22 -0
  122. package/src/plugin.helper.js +26 -0
  123. package/src/plugin.helper.js.map +1 -0
  124. package/src/plugin.helper.ts +42 -0
  125. package/src/promise.helper.d.ts +13 -0
  126. package/src/promise.helper.js +71 -0
  127. package/src/promise.helper.js.map +1 -0
  128. package/src/promise.helper.ts +65 -0
  129. package/src/returnValue.d.ts +14 -0
  130. package/src/returnValue.js +17 -0
  131. package/src/returnValue.js.map +1 -0
  132. package/src/returnValue.ts +26 -0
  133. package/src/rxjs.helper.d.ts +7 -0
  134. package/src/rxjs.helper.js +33 -0
  135. package/src/rxjs.helper.js.map +1 -0
  136. package/src/rxjs.helper.ts +71 -0
  137. package/src/simple-mapper.interceptor.d.ts +7 -0
  138. package/src/simple-mapper.interceptor.js +29 -0
  139. package/src/simple-mapper.interceptor.js.map +1 -0
  140. package/src/simple-mapper.interceptor.ts +43 -0
  141. package/src/validator-helper.d.ts +2 -0
  142. package/src/validator-helper.js +22 -0
  143. package/src/validator-helper.js.map +1 -0
  144. package/src/validator-helper.ts +26 -0
  145. package/src/zlib.helper.d.ts +3 -0
  146. package/src/zlib.helper.js +25 -0
  147. package/src/zlib.helper.js.map +1 -0
  148. package/src/zlib.helper.ts +19 -0
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@nest-yalc-2/utils",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "./src/index.js",
6
+ "exports": {
7
+ "./": "./src/"
8
+ },
9
+ "author": "",
10
+ "license": "AGPL-3.0-or-later",
11
+ "types": "./src/index.d.ts",
12
+ "files": [
13
+ "src"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/Nek97/nestjs-yalc.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/Nek97/nestjs-yalc/issues"
21
+ },
22
+ "homepage": "https://github.com/Nek97/nestjs-yalc#readme",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "dependencies": {
27
+ "tslib": "^2.8.1"
28
+ }
29
+ }
@@ -0,0 +1,9 @@
1
+ export enum TestEnum1 {
2
+ TEST1 = 'test1',
3
+ TEST2 = 'test2',
4
+ }
5
+
6
+ export enum TestEnum2 {
7
+ TEST2 = 'test2',
8
+ TEST3 = 'test3',
9
+ }
@@ -0,0 +1,32 @@
1
+ import { isClass, isES6Class } from '../class.helper.js';
2
+ import { describe, expect, it } from '@jest/globals';
3
+ import { DefaultError } from '@nest-yalc-2/errors/default.error.js';
4
+
5
+ describe('Test Class Helpers', () => {
6
+ it('should test a class correctly', () => {
7
+ expect(isClass(class {})).toBeTruthy();
8
+ });
9
+
10
+ it('should test a class correctly', () => {
11
+ expect(
12
+ isClass(function (nothing: any) {
13
+ nothing;
14
+ }),
15
+ ).toBeTruthy();
16
+ expect(isClass(Error)).toBeTruthy();
17
+ expect(isES6Class(Error)).toBeFalsy();
18
+ expect(isClass(() => {})).toBeFalsy();
19
+ });
20
+
21
+ it('expect an instance of a class to not be a class', () => {
22
+ expect(isClass(new (class {})())).toBeFalsy();
23
+ });
24
+
25
+ it('should test a class against a name', () => {
26
+ const defaultError = DefaultError;
27
+ expect(isClass(class Test {}, 'Test')).toBeTruthy();
28
+ expect(isClass(class Test {}, 'Test2')).toBeFalsy();
29
+ expect(isES6Class(class Test {}, 'Test2')).toBeFalsy();
30
+ expect(isClass(defaultError, DefaultError.name)).toBeTruthy();
31
+ });
32
+ });
@@ -0,0 +1,35 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import { commandWithErrors } from '../command.helper.js';
12
+
13
+ describe('test shared functions', () => {
14
+ it('should run cliWithErrors without errors', async () => {
15
+ const fn = jest.fn();
16
+ const cliWithErrorsResult = commandWithErrors(fn);
17
+ await cliWithErrorsResult(1);
18
+
19
+ expect(fn).toHaveBeenCalledTimes(1);
20
+ expect(fn).toHaveBeenLastCalledWith(1);
21
+ });
22
+
23
+ it('should run cliWithErrors and call the process exit', async () => {
24
+ const spiedProcessExit = jest.spyOn(process, 'exit');
25
+
26
+ spiedProcessExit.mockImplementation((() => {}) as any);
27
+
28
+ await commandWithErrors(async () => {
29
+ throw new Error('Test');
30
+ })('Test');
31
+ expect(spiedProcessExit).toHaveBeenCalledWith(1);
32
+
33
+ spiedProcessExit.mockRestore();
34
+ });
35
+ });
@@ -0,0 +1,93 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import { ConfigValueManager, checkForDuplicateKeys } from '../config-manager.helper.js'; // Adjust the import path accordingly
3
+
4
+ describe('ConfigValueManager and Utilities', () => {
5
+
6
+ describe('checkForDuplicateKeys', () => {
7
+ it('should not throw an error for unique keys', () => {
8
+ const execute = () => checkForDuplicateKeys(['key1', 'key2', 'key3']);
9
+ expect(execute).not.toThrow();
10
+ });
11
+
12
+ it('should throw an error for duplicate keys', () => {
13
+ const execute = () => checkForDuplicateKeys(['key1', 'key1']);
14
+ expect(execute).toThrow(/Duplicate key found in configuration: key1/);
15
+ });
16
+ });
17
+
18
+ describe('ConfigValueManager.value', () => {
19
+ it('returns the correct value for a matching key', () => {
20
+ const result = ConfigValueManager.value('myKey', {k: 'myKey', v: 'myValue'});
21
+ expect(result).toBe('myValue');
22
+ });
23
+
24
+ it('returns the default value when key does not match', () => {
25
+ const result = ConfigValueManager.value('otherKey', { k: 'myKey', v: 'myValue' }, 'default');
26
+ expect(result).toBe('default');
27
+ });
28
+
29
+ it('returns the value from a function when key matches', () => {
30
+ const result = ConfigValueManager.value('myKey', { k: 'myKey', v: () => 'dynamicValue' });
31
+ expect(result).toBe('dynamicValue');
32
+ });
33
+
34
+ it('returns the default value from a function when key does not match', () => {
35
+ const result = ConfigValueManager.value('otherKey', { k: 'myKey', v: 'myValue' }, () => 'dynamicDefault');
36
+ expect(result).toBe('dynamicDefault');
37
+ });
38
+
39
+ it('handles multiple configurations and returns the first matching value', () => {
40
+ const configs = [{ k: 'myKey1', v: 'value1' }, { k: 'myKey2', v: 'value2' }];
41
+ const result = ConfigValueManager.value('myKey2', configs);
42
+ expect(result).toBe('value2');
43
+ });
44
+
45
+ it('throws an error for duplicate keys in configurations', () => {
46
+ const configs = [{ k: 'myKey', v:'value1'}, { k: 'myKey', v: 'value2'}];
47
+ const execute = () => ConfigValueManager.value('myKey', configs);
48
+ expect(execute).toThrow(/Duplicate key found in configuration: myKey/);
49
+ });
50
+ });
51
+
52
+ describe('ConfigValueManager.is', () => {
53
+ it('returns true when the key matches', () => {
54
+ const result = ConfigValueManager.is('myKey', 'myKey');
55
+ expect(result).toBe(true);
56
+ });
57
+
58
+ it('returns false when the key does not match', () => {
59
+ const result = ConfigValueManager.is('otherKey', 'myKey');
60
+ expect(result).toBe(false);
61
+ });
62
+
63
+ it('returns false when using negation and the key matches', () => {
64
+ const result = ConfigValueManager.is('myKey', 'myKey', true);
65
+ expect(result).toBe(false);
66
+ });
67
+ });
68
+
69
+ describe('ConfigValueManager.only', () => {
70
+ it('returns the value for a matching key', () => {
71
+ const result = ConfigValueManager.only('myKey', 'myKey', 'returnValue');
72
+ expect(result).toBe('returnValue');
73
+ });
74
+
75
+ it('returns undefined for a non-matching key', () => {
76
+ const result = ConfigValueManager.only('otherKey', 'myKey', 'returnValue');
77
+ expect(result).toBeUndefined();
78
+ });
79
+ });
80
+
81
+ describe('ConfigValueManager.skip', () => {
82
+ it('returns undefined for a matching key', () => {
83
+ const result = ConfigValueManager.skip('myKey', 'myKey', 'returnValue');
84
+ expect(result).toBeUndefined();
85
+ });
86
+
87
+ it('returns the value for a non-matching key', () => {
88
+ const result = ConfigValueManager.skip('otherKey', 'myKey', 'returnValue');
89
+ expect(result).toBe('returnValue');
90
+ });
91
+ });
92
+
93
+ });
@@ -0,0 +1,34 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+ import { stringIsInEnumValidatorFactory } from '../custom-validator.js';
11
+
12
+ describe('stringIsInEnumValidatorFactory', () => {
13
+ enum TestEnum {
14
+ Value1 = 'Value1',
15
+ Value2 = 'Value2',
16
+ }
17
+
18
+ it('should validate if a string is in the enum', () => {
19
+ const validator = stringIsInEnumValidatorFactory(TestEnum);
20
+
21
+ const validString = 'Value1';
22
+ const invalidString = 'Invalid';
23
+
24
+ expect(validator.validate(validString)).toBe(true);
25
+ expect(validator.validate(invalidString)).toBe(false);
26
+ });
27
+
28
+ it('should call stringIsInEnum with the right arguments', () => {
29
+ const validator = stringIsInEnumValidatorFactory(TestEnum);
30
+
31
+ const validString = 'Value1';
32
+ expect(validator.validate(validString)).toBe(true);
33
+ });
34
+ });
@@ -0,0 +1,30 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import { arrayGroupByField } from '../data-structure.helper.js';
12
+
13
+ const testArray = [
14
+ {
15
+ key: 'xx',
16
+ data: 'firstData',
17
+ },
18
+ {
19
+ key: 'xx',
20
+ data: 'seconData',
21
+ },
22
+ ];
23
+ describe('Test Data Structure Helpers', () => {
24
+ it('should groupby an array with a field', () => {
25
+ const groupedArray = arrayGroupByField(testArray, (item) => item.key);
26
+ expect(groupedArray['xx']).toEqual(
27
+ expect.arrayContaining([testArray[0], testArray[1]]),
28
+ );
29
+ });
30
+ });
@@ -0,0 +1,78 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ } from '@jest/globals';
9
+
10
+ import { DateHelper } from '../date.helper.js';
11
+
12
+ describe('Date helper test suite', () => {
13
+ it('getDateQuery - returns proper query', () => {
14
+ jest
15
+ .spyOn(global.Date, 'now')
16
+ .mockImplementation(() => Date.parse('01-30-2022'));
17
+
18
+ const result = DateHelper.getWeekEdgeDates();
19
+
20
+ expect(result).toEqual({ endDate: '2022-01-30', startDate: '2022-01-23' });
21
+ });
22
+
23
+ it('getDateQuery - gets months with single number code', () => {
24
+ jest
25
+ .spyOn(global.Date, 'now')
26
+ .mockImplementation(() => Date.parse('09-09-2022'));
27
+
28
+ const result = DateHelper.getWeekEdgeDates();
29
+
30
+ expect(result).toEqual({ endDate: '2022-09-09', startDate: '2022-09-02' });
31
+ });
32
+
33
+ it('getDateQuery - gets month with double number code', () => {
34
+ jest
35
+ .spyOn(global.Date, 'now')
36
+ .mockImplementation(() => Date.parse('12-09-2022'));
37
+
38
+ const result = DateHelper.getWeekEdgeDates();
39
+
40
+ expect(result).toEqual({ endDate: '2022-12-09', startDate: '2022-12-02' });
41
+ });
42
+
43
+ it('getDateQuery - returns query with week overlapping on two months', () => {
44
+ jest
45
+ .spyOn(global.Date, 'now')
46
+ .mockImplementation(() => Date.parse('12-04-2022'));
47
+
48
+ const result = DateHelper.getWeekEdgeDates();
49
+
50
+ expect(result).toEqual({ endDate: '2022-12-04', startDate: '2022-11-27' });
51
+ });
52
+
53
+ it('should be able add minutes to a Date', () => {
54
+ const date = new Date('2021-10-13T13:13:00.000Z');
55
+ const result = DateHelper.addMinutes(date, 5);
56
+ expect(result).toBeInstanceOf(Date);
57
+ expect(result.toISOString()).toBe('2021-10-13T13:18:00.000Z');
58
+ });
59
+
60
+ it('should be able substract minutes from a Date', () => {
61
+ const date = new Date('2003-01-05T20:00:00.000Z');
62
+ const result = DateHelper.subsMinutes(date, 5);
63
+ expect(result).toBeInstanceOf(Date);
64
+ expect(result.toISOString()).toBe('2003-01-05T19:55:00.000Z');
65
+ });
66
+
67
+ it('should be able to convert to SQL format', () => {
68
+ const date = new Date('2003-01-05T20:00:00.000Z');
69
+ const result = DateHelper.dateToSQLDateTime(date);
70
+ expect(result).toBe('2003-01-05 20:00:00');
71
+ });
72
+
73
+ it('dateToPlainISO - converts date to plain ISO format', () => {
74
+ const date = new Date('2003-01-05T20:00:00.000Z');
75
+ const result = DateHelper.dateToPlainISO(date);
76
+ expect(result).toBe('20030105T200000000Z');
77
+ });
78
+ });
@@ -0,0 +1,45 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import { encryptAes, decryptAes } from '../encryption.helper.js';
12
+ import crypto from 'crypto';
13
+
14
+ // Please let me know if you know how to solve this as unknown as void.
15
+ jest
16
+ .spyOn(crypto, 'randomBytes')
17
+ .mockReturnValue(
18
+ Buffer.from('173606b5fe22770c890b48c41866abc3', 'hex') as unknown as void,
19
+ );
20
+
21
+ describe('Utils encryption helper test', () => {
22
+ const key =
23
+ '60f810732e3c8d1eb5ba227143d46aabce1bb1ae0aa7081ef0b0c3bbaaed76a6';
24
+ const decrypted = 'should be able to encrypt a simple message with a key';
25
+ it('should be defined', () => {
26
+ expect(encryptAes).toBeDefined();
27
+ });
28
+
29
+ it('should be defined', () => {
30
+ expect(decryptAes).toBeDefined();
31
+ });
32
+
33
+ it('should be able to encrypt a simple message with a key', () => {
34
+ expect(encryptAes(decrypted, key)).toBeDefined();
35
+ });
36
+
37
+ it('should be able to decrypt a simple message with a key', () => {
38
+ const encrypted = encryptAes(decrypted, key);
39
+ expect(decryptAes(encrypted, key)).toEqual(decrypted);
40
+ });
41
+
42
+ it('should return an empty string if the input is empty as well', () => {
43
+ expect(decryptAes('', key)).toEqual('');
44
+ });
45
+ });
@@ -0,0 +1,53 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import {
12
+ belongsToEnum,
13
+ mergeEnums,
14
+ getEnumValueByEnumKey,
15
+ } from '../enum.helper.js';
16
+ import { TestEnum1, TestEnum2 } from '../__mocks__/enum.mock.js';
17
+
18
+ test('belongsToEnum should check if a value belongs to a specific Enum', () => {
19
+ enum TestEnum {
20
+ TEST = 'test',
21
+ }
22
+
23
+ let result = belongsToEnum(TestEnum, 'test');
24
+ expect(result).toBeTruthy();
25
+
26
+ result = belongsToEnum(TestEnum, 'TEST');
27
+ expect(result).toBeFalsy();
28
+ });
29
+
30
+ test('mergeEnums should merge 2 enums', () => {
31
+ const result = mergeEnums(TestEnum1, TestEnum2);
32
+ const compareWith = {
33
+ ...TestEnum1,
34
+ ...TestEnum2,
35
+ };
36
+
37
+ expect(result).toMatchObject(compareWith);
38
+ });
39
+
40
+ test('getEnumValueByEnumKey should return value of enum 1 by key of enum 2', () => {
41
+ const result = getEnumValueByEnumKey(TestEnum1, Object.keys(TestEnum2)[0]);
42
+ expect(result).toBe('test2');
43
+ });
44
+
45
+ test('getEnumValueByEnumKey should return value of enum 1 by key of enum 2', () => {
46
+ const result = getEnumValueByEnumKey(TestEnum1, Object.keys(TestEnum2)[1]);
47
+ expect(result).toBeUndefined();
48
+ });
49
+
50
+ test('getEnumValueByEnumKey should return null when no undefined key is provided ', () => {
51
+ const result = getEnumValueByEnumKey(TestEnum1, undefined);
52
+ expect(result).toBeUndefined();
53
+ });
@@ -0,0 +1,68 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import { envIsTrue, envToArray, isProduction } from '../env.helper.js';
12
+ import { envTestHelper } from '@nest-yalc-2/jest';
13
+
14
+ describe('environment helper test', () => {
15
+ it('should convert a comma separated list to an array', () => {
16
+ process.env.Test = 'apple,pear,pineapple,cherry';
17
+ const res = envToArray('Test');
18
+ expect(res).toEqual(['apple', 'pear', 'pineapple', 'cherry']);
19
+ });
20
+
21
+ it('should return an empty array when the var is an empty string', () => {
22
+ process.env.Test = '';
23
+ const res = envToArray('Test');
24
+ expect(res).toEqual([]);
25
+ });
26
+
27
+ it('should return an empty array when the var is missing', () => {
28
+ delete process.env.Test;
29
+ const res = envToArray('Test');
30
+ expect(res).toEqual([]);
31
+ });
32
+
33
+ it('should return false for "undefined"', () => {
34
+ expect(envIsTrue(undefined)).toEqual(false);
35
+ });
36
+
37
+ it('should return true for "true"', () => {
38
+ expect(envIsTrue('true')).toEqual(true);
39
+ });
40
+
41
+ it('should return true for "1"', () => {
42
+ expect(envIsTrue('1')).toEqual(true);
43
+ });
44
+
45
+ it('should return true for "on"', () => {
46
+ expect(envIsTrue('on')).toEqual(true);
47
+ });
48
+
49
+ it('should return true if is production"', () => {
50
+ const env = envTestHelper(process.env);
51
+ env.setEnv('NODE_ENV', 'production');
52
+ expect(isProduction(false)).toBeTruthy();
53
+
54
+ env.setEnv('NODE_ENV', 'somethingelse');
55
+ expect(isProduction(true)).toBeTruthy();
56
+ env.reset();
57
+ });
58
+
59
+ it('should return false if is not production"', () => {
60
+ const env = envTestHelper(process.env);
61
+ env.setEnv('NODE_ENV', 'pipeline');
62
+ expect(isProduction()).toBeFalsy();
63
+
64
+ env.setEnv('NODE_ENV', 'somethingelse');
65
+ expect(isProduction(false)).toBeFalsy();
66
+ env.reset();
67
+ });
68
+ });
@@ -0,0 +1,20 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+ import { throwWrap } from '../error.helper.js';
11
+
12
+ describe('Test errors helpers', () => {
13
+ it('should throw an error without wraping it', () => {
14
+ const fixedErrorMessage = `i'm an error`;
15
+ const fixedError = new Error(fixedErrorMessage);
16
+
17
+ expect(() => throwWrap(fixedErrorMessage)).toThrow(fixedErrorMessage);
18
+ expect(() => throwWrap(fixedError)).toThrow(fixedErrorMessage);
19
+ });
20
+ });