@unchainedshop/utils 3.1.1 → 3.1.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/utils",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "main": "lib/utils-index.js",
5
5
  "types": "lib/utils-index.d.ts",
6
6
  "type": "module",
@@ -9,8 +9,8 @@
9
9
  "build": "tsc -b",
10
10
  "prepublishOnly": "npm run clean && npm run build",
11
11
  "watch": "tsc -w",
12
- "test": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --forceExit",
13
- "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --detectOpenHandles --forceExit"
12
+ "test": "tsx --test",
13
+ "test:watch": "tsx --test --watch"
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
@@ -33,7 +33,6 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^22.13.4",
36
- "jest": "^29.7.0",
37
36
  "typescript": "^5.7.3"
38
37
  }
39
38
  }
@@ -1,3 +1,6 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert';
3
+
1
4
  import {
2
5
  applyRate,
3
6
  roundToNext,
@@ -8,63 +11,63 @@ import {
8
11
 
9
12
  describe('roundToNext', () => {
10
13
  it('rounds to the next multiple of precision correctly when the value is positive', () => {
11
- expect(roundToNext(5, 2)).toBe(6);
12
- expect(roundToNext(10, 5)).toBe(10);
13
- expect(roundToNext(13, 5)).toBe(15);
14
+ assert.strictEqual(roundToNext(5, 2), 6);
15
+ assert.strictEqual(roundToNext(10, 5), 10);
16
+ assert.strictEqual(roundToNext(13, 5), 15);
14
17
  });
15
18
 
16
19
  it('rounds to the next multiple of precision correctly when the value is negative', () => {
17
- expect(roundToNext(-5, 2)).toBe(-4);
18
- expect(roundToNext(-10, 5)).toBe(-10);
19
- expect(roundToNext(-13, 5)).toBe(-10);
20
+ assert.strictEqual(roundToNext(-5, 2), -4);
21
+ assert.strictEqual(roundToNext(-10, 5), -10);
22
+ assert.strictEqual(roundToNext(-13, 5), -10);
20
23
  });
21
24
 
22
25
  it('returns 0 when the value is 0', () => {
23
- expect(roundToNext(0, 5)).toBe(0);
26
+ assert.strictEqual(roundToNext(0, 5), 0);
24
27
  });
25
28
 
26
29
  it('returns the same value when the precision is 1', () => {
27
- expect(roundToNext(5, 1)).toBe(5);
30
+ assert.strictEqual(roundToNext(5, 1), 5);
28
31
  });
29
32
  });
30
33
 
31
34
  describe('resolveAmountAndTax', () => {
32
35
  it('ratio is 0 and taxDivisor is 0', () => {
33
36
  const result = resolveAmountAndTax({ ratio: 0, taxDivisor: 0 }, 100);
34
- expect(result).toEqual([0, 0]);
37
+ assert.deepStrictEqual(result, [0, 0]);
35
38
  });
36
39
 
37
40
  it('ratio is not 0 and taxDivisor is 0', () => {
38
41
  const result = resolveAmountAndTax({ ratio: 0.5, taxDivisor: 0 }, 100);
39
- expect(result).toEqual([50, 0]);
42
+ assert.deepStrictEqual(result, [50, 0]);
40
43
  });
41
44
 
42
45
  it('ratio is 0 and taxDivisor is not 0', () => {
43
46
  const result = resolveAmountAndTax({ ratio: 0, taxDivisor: 2 }, 100);
44
- expect(result).toEqual([0, 0]);
47
+ assert.deepStrictEqual(result, [0, 0]);
45
48
  });
46
49
 
47
50
  it('ratio and taxDivisor are not 0', () => {
48
51
  const result = resolveAmountAndTax({ ratio: 0.5, taxDivisor: 2 }, 100);
49
- expect(result).toEqual([50, 25]);
52
+ assert.deepStrictEqual(result, [50, 25]);
50
53
  });
51
54
 
52
55
  it('ratio and taxDivisor are not finite number', () => {
53
56
  const result = resolveAmountAndTax({ ratio: Number.NaN, taxDivisor: Number.NaN }, 100);
54
- expect(result).toEqual([0, 0]);
57
+ assert.deepStrictEqual(result, [0, 0]);
55
58
  });
56
59
  });
57
60
 
58
- describe('resolveAmountAndTax', () => {
61
+ describe('applyDiscountToMultipleShares', () => {
59
62
  it('shares are empty', () => {
60
63
  const result = applyDiscountToMultipleShares([], 100);
61
- expect(result).toEqual([0, 0]);
64
+ assert.deepStrictEqual(result, [0, 0]);
62
65
  });
63
66
 
64
67
  it('shares have one share', () => {
65
68
  const shares = [{ ratio: 0.5, taxDivisor: 2 }];
66
69
  const result = applyDiscountToMultipleShares(shares, 100);
67
- expect(result).toEqual([50, 25]);
70
+ assert.deepStrictEqual(result, [50, 25]);
68
71
  });
69
72
 
70
73
  it('shares have multiple shares', () => {
@@ -73,7 +76,7 @@ describe('resolveAmountAndTax', () => {
73
76
  { ratio: 0.2, taxDivisor: 1.5 },
74
77
  ];
75
78
  const result = applyDiscountToMultipleShares(shares, 100);
76
- expect(result).toEqual([70, 31.666666666666664]);
79
+ assert.deepStrictEqual(result, [70, 31.666666666666664]);
77
80
  });
78
81
  });
79
82
 
@@ -83,7 +86,7 @@ describe('applyRate', () => {
83
86
  rate: 0.1,
84
87
  };
85
88
  const amount = 100;
86
- expect(applyRate(configuration, amount)).toBe(10);
89
+ assert.strictEqual(applyRate(configuration, amount), 10);
87
90
  });
88
91
 
89
92
  it('applies the fixed rate correctly when a fixed rate is provided', () => {
@@ -91,7 +94,7 @@ describe('applyRate', () => {
91
94
  fixedRate: 50,
92
95
  };
93
96
  const amount = 100;
94
- expect(applyRate(configuration, amount)).toBe(50);
97
+ assert.strictEqual(applyRate(configuration, amount), 50);
95
98
  });
96
99
 
97
100
  it('applies the fixed rate correctly when a fixed rate is provided and the amount is less than fixed rate', () => {
@@ -99,7 +102,7 @@ describe('applyRate', () => {
99
102
  fixedRate: 150,
100
103
  };
101
104
  const amount = 100;
102
- expect(applyRate(configuration, amount)).toBe(100);
105
+ assert.strictEqual(applyRate(configuration, amount), 100);
103
106
  });
104
107
 
105
108
  it('applies the rate correctly when rate is less than or equal to 0', () => {
@@ -107,44 +110,7 @@ describe('applyRate', () => {
107
110
  rate: -0.1,
108
111
  };
109
112
  const amount = 100;
110
- expect(applyRate(configuration, amount)).toBe(-10);
111
- });
112
- it('applies the rate correctly when only rate is provided and amount is negative', () => {
113
- const configuration = {
114
- rate: 0.1,
115
- };
116
- const amount = -100;
117
- expect(applyRate(configuration, amount)).toBe(-10);
118
- });
119
-
120
- it('applies the rate correctly when only rate is provided and amount is negative', () => {
121
- const configuration = {
122
- rate: 0.1,
123
- };
124
- const amount = -100;
125
- expect(applyRate(configuration, amount)).toBe(-10);
126
- });
127
-
128
- it('applies the rate correctly when only rate is provided and amount is negative', () => {
129
- const configuration = {
130
- rate: 0.1,
131
- };
132
- const amount = -100;
133
- expect(applyRate(configuration, amount)).toBe(-10);
134
- });
135
-
136
- it('applies the rate correctly when only rate is provided and amount is negative', () => {
137
- const configuration = {
138
- rate: 0.1,
139
- };
140
- const amount = -100;
141
- expect(applyRate(configuration, amount)).toBe(-10);
142
- });
143
-
144
- it('returns 0 when rate and fixed rate are not provided', () => {
145
- const configuration = {};
146
- const amount = 100;
147
- expect(applyRate(configuration, amount)).toBe(0);
113
+ assert.strictEqual(applyRate(configuration, amount), -10);
148
114
  });
149
115
 
150
116
  it('applies the rate correctly when only rate is provided and amount is negative', () => {
@@ -152,13 +118,13 @@ describe('applyRate', () => {
152
118
  rate: 0.1,
153
119
  };
154
120
  const amount = -100;
155
- expect(applyRate(configuration, amount)).toBe(-10);
121
+ assert.strictEqual(applyRate(configuration, amount), -10);
156
122
  });
157
123
 
158
124
  it('returns 0 when rate and fixed rate are not provided', () => {
159
125
  const configuration = {};
160
126
  const amount = 100;
161
- expect(applyRate(configuration, amount)).toBe(0);
127
+ assert.strictEqual(applyRate(configuration, amount), 0);
162
128
  });
163
129
  });
164
130
 
@@ -168,7 +134,7 @@ describe('calculateAmountToSplit', () => {
168
134
  rate: 0.1,
169
135
  };
170
136
  const amount = 1000;
171
- expect(calculateAmountToSplit(configuration, amount)).toBe(100);
137
+ assert.strictEqual(calculateAmountToSplit(configuration, amount), 100);
172
138
  });
173
139
 
174
140
  it('calculates the correct amount to split when using a fixed rate', () => {
@@ -176,7 +142,7 @@ describe('calculateAmountToSplit', () => {
176
142
  fixedRate: 50,
177
143
  };
178
144
  const amount = 1000;
179
- expect(calculateAmountToSplit(configuration, amount)).toBe(50);
145
+ assert.strictEqual(calculateAmountToSplit(configuration, amount), 50);
180
146
  });
181
147
 
182
148
  it('returns 0 when the amount is less than or equal to 0', () => {
@@ -184,7 +150,7 @@ describe('calculateAmountToSplit', () => {
184
150
  rate: 0.1,
185
151
  };
186
152
  const amount = 0;
187
- expect(calculateAmountToSplit(configuration, amount)).toBe(0);
153
+ assert.strictEqual(calculateAmountToSplit(configuration, amount), 0);
188
154
  });
189
155
 
190
156
  it('returns 0 when amount is negative', () => {
@@ -192,7 +158,7 @@ describe('calculateAmountToSplit', () => {
192
158
  rate: 0.1,
193
159
  };
194
160
  const amount = -1000;
195
- expect(calculateAmountToSplit(configuration, amount)).toBe(0);
161
+ assert.strictEqual(calculateAmountToSplit(configuration, amount), 0);
196
162
  });
197
163
 
198
164
  it('returns 0 when configuration rate is negative', () => {
@@ -200,6 +166,6 @@ describe('calculateAmountToSplit', () => {
200
166
  rate: -0.1,
201
167
  };
202
168
  const amount = 1000;
203
- expect(calculateAmountToSplit(configuration, amount)).toBe(0);
169
+ assert.strictEqual(calculateAmountToSplit(configuration, amount), 0);
204
170
  });
205
171
  });
@@ -1,24 +1,26 @@
1
+ import { describe, it, beforeEach } from 'node:test';
2
+ import assert from 'node:assert';
1
3
  import { BaseDirector } from './BaseDirector.js';
2
4
 
3
5
  const adapter1 = {
4
6
  key: 'adapter1',
5
7
  label: 'Adapter 1',
6
8
  version: '1',
7
- log: import.meta.jest.fn(),
9
+ log: () => ({}),
8
10
  };
9
11
 
10
12
  const adapter2 = {
11
13
  key: 'adapter2',
12
14
  label: 'Adapter 2',
13
15
  version: '2',
14
- log: import.meta.jest.fn(),
16
+ log: () => ({}),
15
17
  };
16
18
 
17
19
  const adapter3 = {
18
20
  key: 'adapter3',
19
21
  label: 'Adapter 3',
20
22
  version: '3',
21
- log: import.meta.jest.fn(),
23
+ log: () => ({}),
22
24
  };
23
25
 
24
26
  describe('BaseDirector', () => {
@@ -34,21 +36,16 @@ describe('BaseDirector', () => {
34
36
  describe('registerAdapter', () => {
35
37
  it('should add the adapter to the director', () => {
36
38
  director.registerAdapter(adapter1);
37
- expect(director.getAdapter(adapter1.key)).toBe(adapter1);
38
- });
39
-
40
- xit('should generate a log message when an adapter is registered', () => {
41
- const spy = import.meta.jest.spyOn(console, 'log');
42
- expect(spy).toHaveBeenCalledWith('TestDirector -> Registered adapter1 1.0.0 (Adapter 1)');
39
+ assert.strictEqual(director.getAdapter(adapter1.key), adapter1);
43
40
  });
44
41
 
45
42
  it('should allow multiple adapters to be registered', () => {
46
- expect(director.getAdapter(adapter1.key)).toBe(adapter1);
47
- expect(director.getAdapter(adapter2.key)).toBe(adapter2);
43
+ assert.strictEqual(director.getAdapter(adapter1.key), adapter1);
44
+ assert.strictEqual(director.getAdapter(adapter2.key), adapter2);
48
45
  });
49
46
 
50
47
  it('should register an adapter and add it to the internal map', () => {
51
- expect(director.getAdapter(adapter1.key)).toEqual(adapter1);
48
+ assert.deepStrictEqual(director.getAdapter(adapter1.key), adapter1);
52
49
  });
53
50
 
54
51
  it('should use the adapterKeyField specified in the options to store the adapter in the internal map', () => {
@@ -56,7 +53,7 @@ describe('BaseDirector', () => {
56
53
  adapterKeyField: 'label',
57
54
  });
58
55
  director.registerAdapter(adapter1);
59
- expect(director.getAdapter(adapter1.label)).toEqual(adapter1);
56
+ assert.deepStrictEqual(director.getAdapter(adapter1.label), adapter1);
60
57
  });
61
58
 
62
59
  it('should return the correct Adapter object', () => {
@@ -65,46 +62,43 @@ describe('BaseDirector', () => {
65
62
  director = BaseDirector(directorName, { adapterKeyField });
66
63
  director.registerAdapter(adapter1);
67
64
 
68
- expect(director.getAdapter(adapter1.key)).toEqual(adapter1);
65
+ assert.deepStrictEqual(director.getAdapter(adapter1.key), adapter1);
69
66
  });
70
67
 
71
68
  it('should return undefined if the Adapter does not exist', () => {
72
69
  const directorName = 'TestDirector';
73
70
  const adapterKeyField = 'key';
74
71
  director = BaseDirector(directorName, { adapterKeyField });
75
- expect(director.getAdapter(adapter1.key)).toBeUndefined();
72
+ assert.strictEqual(director.getAdapter(adapter1.key), undefined);
76
73
  });
77
74
  });
78
75
 
79
76
  describe('getAdapter', () => {
80
77
  it('should return the correct Adapter object for a given key', () => {
81
- // Expect that calling getAdapter with the key 'foo' returns the fooAdapter
82
- expect(director.getAdapter(adapter1.key)).toBe(adapter1);
78
+ assert.strictEqual(director.getAdapter(adapter1.key), adapter1);
83
79
  });
84
80
 
85
81
  it('should return undefined if no Adapter is registered with the given key', () => {
86
- // Expect that calling getAdapter with a key that has not been registered returns undefined
87
- expect(director.getAdapter('invalid-key')).toBeUndefined();
82
+ assert.strictEqual(director.getAdapter('invalid-key'), undefined);
88
83
  });
89
84
 
90
85
  it('should return the correct Adapter object when the keyField is set to a property other than "key"', () => {
91
- // Create a director with the keyField option set to 'label'
92
86
  director = BaseDirector('TestDirector', { adapterKeyField: 'label' });
93
87
  director.registerAdapter(adapter1);
94
- expect(director.getAdapter(adapter1.label)).toBe(adapter1);
88
+ assert.strictEqual(director.getAdapter(adapter1.label), adapter1);
95
89
  });
96
90
  });
97
91
 
98
92
  describe('getAdapters', () => {
99
93
  it('should return an array of registered Adapter objects', () => {
100
- expect(director.getAdapters()).toEqual([adapter1, adapter3, adapter2]);
94
+ assert.deepStrictEqual(director.getAdapters(), [adapter1, adapter3, adapter2]);
101
95
  });
102
96
 
103
97
  it('should return an empty array if no Adapter objects have been registered', () => {
104
98
  const directorName = 'TestDirector';
105
99
  const adapterKeyField = 'key';
106
100
  const baseDirector = BaseDirector(directorName, { adapterKeyField });
107
- expect(baseDirector.getAdapters()).toEqual([]);
101
+ assert.deepStrictEqual(baseDirector.getAdapters(), []);
108
102
  });
109
103
 
110
104
  it('should return an array of registered Adapter objects sorted by the adapterSortKey provided', () => {
@@ -112,7 +106,7 @@ describe('BaseDirector', () => {
112
106
  director.registerAdapter(adapter1);
113
107
  director.registerAdapter(adapter3);
114
108
  director.registerAdapter(adapter2);
115
- expect(director.getAdapters()).toEqual([adapter1, adapter2, adapter3]);
109
+ assert.deepStrictEqual(director.getAdapters(), [adapter1, adapter2, adapter3]);
116
110
  });
117
111
  });
118
112
  });
@@ -1,19 +1,21 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert';
1
3
  import { resolveBestCountry, resolveBestSupported, slugify, systemLocale } from './utils-index.js';
2
4
  import generateHashId from './generate-random-hash.js';
3
5
 
4
6
  describe('Utils', () => {
5
7
  it('Locale', () => {
6
- expect(systemLocale).toBeDefined();
7
- expect(resolveBestCountry).toBeInstanceOf(Function);
8
- expect(resolveBestSupported).toBeInstanceOf(Function);
8
+ assert.ok(systemLocale);
9
+ assert.equal(typeof resolveBestCountry, 'function');
10
+ assert.equal(typeof resolveBestSupported, 'function');
9
11
  });
10
12
 
11
13
  describe('generateHashId', () => {
12
- it('should create a random hash ', () => {
14
+ it('should create a random hash', () => {
13
15
  const result = generateHashId();
14
16
 
15
- expect(typeof result).toBe('string');
16
- expect(result).toMatch(/^[A-Z0-9]+$/);
17
+ assert.equal(typeof result, 'string');
18
+ assert.match(result, /^[A-Z0-9]+$/);
17
19
  });
18
20
  });
19
21
 
@@ -24,7 +26,7 @@ describe('Utils', () => {
24
26
 
25
27
  const result = slugify(text);
26
28
 
27
- expect(result).toEqual(expected);
29
+ assert.equal(result, expected);
28
30
  });
29
31
 
30
32
  it('with a string containing special characters', () => {
@@ -33,7 +35,7 @@ describe('Utils', () => {
33
35
 
34
36
  const result = slugify(text);
35
37
 
36
- expect(result).toEqual(expected);
38
+ assert.equal(result, expected);
37
39
  });
38
40
 
39
41
  it('with a string containing multiple spaces', () => {
@@ -42,7 +44,7 @@ describe('Utils', () => {
42
44
 
43
45
  const result = slugify(text);
44
46
 
45
- expect(result).toEqual(expected);
47
+ assert.equal(result, expected);
46
48
  });
47
49
 
48
50
  it('with a string containing only special characters (leave underscore)', () => {
@@ -51,7 +53,7 @@ describe('Utils', () => {
51
53
 
52
54
  const result = slugify(text);
53
55
 
54
- expect(result).toEqual(expected);
56
+ assert.equal(result, expected);
55
57
  });
56
58
  });
57
59
  });
package/jest.config.js DELETED
@@ -1,17 +0,0 @@
1
- /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
- export default {
3
- preset: 'ts-jest/presets/default-esm', // or other ESM presets
4
- testEnvironment: 'node',
5
- extensionsToTreatAsEsm: ['.ts'],
6
- transform: {
7
- '^.+\\.tsx?$': [
8
- 'ts-jest',
9
- {
10
- useESM: true,
11
- },
12
- ],
13
- },
14
- moduleNameMapper: {
15
- '^(\\.{1,2}/.*)\\.js$': '$1',
16
- },
17
- };