@unchainedshop/roles 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/lib/roles.d.ts.map +1 -1
- package/lib/roles.js +2 -3
- package/lib/roles.js.map +1 -1
- package/package.json +3 -8
- package/src/roles.test.ts +49 -50
- package/src/roles.ts +2 -3
- package/jest.config.js +0 -17
package/lib/roles.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"roles.d.ts","sourceRoot":"","sources":["../src/roles.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"roles.d.ts","sourceRoot":"","sources":["../src/roles.ts"],"names":[],"mappings":"AAGA,KAAK,eAAe,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,GAAG,CAAA;CAAE,CAAC;AACvD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE;QACV,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;KACrB,CAAC;IACF,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;IACvG,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,oBAAoB;IACnC,KAAK,GAAG,EAAE,MAAM,GAAG,aAAa,CAAC;CAClC;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,CAAC,CAAC;AAE5D,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE;QACL,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,CAAC;KAC/B,CAAC;IACF,OAAO,EAAE,M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I,CAAC,CAAC;IAClG,iBAAiB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;CACnC;AAED,eAAO,MAAM,KAAK,EAAE,cA6EnB,CAAC;AAEF;;GAEG;AACH,qBAAa,IAAK,YAAW,aAAa;IAKrB,IAAI,EAAE,MAAM;IAJ/B,UAAU,EAAE;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC;IAEpC,OAAO,EAAE;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC;gBAEd,IAAI,EAAE,MAAM;IAS/B;;OAEG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;IAqBhC;;OAEG;IACH,KAAK,CAAC,MAAM,KAAA,EAAE,KAAK,KAAA;CAiBpB"}
|
package/lib/roles.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import clone from 'lodash.clone';
|
|
2
1
|
import { has } from './utils/has.js';
|
|
3
2
|
import { isFunction } from './utils/isFunction.js';
|
|
4
3
|
export const Roles = {
|
|
@@ -93,7 +92,7 @@ export class Role {
|
|
|
93
92
|
}
|
|
94
93
|
let helperFn = func;
|
|
95
94
|
if (!isFunction(helperFn)) {
|
|
96
|
-
const clonedValue =
|
|
95
|
+
const clonedValue = structuredClone(helperFn);
|
|
97
96
|
helperFn = () => {
|
|
98
97
|
return clonedValue;
|
|
99
98
|
};
|
|
@@ -115,7 +114,7 @@ export class Role {
|
|
|
115
114
|
}
|
|
116
115
|
let allowFn = allow;
|
|
117
116
|
if (!isFunction(allowFn)) {
|
|
118
|
-
const clonedValue =
|
|
117
|
+
const clonedValue = structuredClone(allowFn);
|
|
119
118
|
allowFn = () => {
|
|
120
119
|
return clonedValue;
|
|
121
120
|
};
|
package/lib/roles.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"roles.js","sourceRoot":"","sources":["../src/roles.ts"],"names":[],"mappings":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EAAE,CAAC;QAC5B,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM,EAAE,KAAK;QACjB,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC1C,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YACpC,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAC/B,CAAC;QACD,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YACzB,MAAM,WAAW,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;YAC7C,OAAO,GAAG,GAAG,EAAE;gBACb,OAAO,WAAW,CAAC;YACrB,CAAC,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACxD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC;CACF;AAED;;GAEG;AACH,KAAK,CAAC,SAAS,GAAG,IAAI,IAAI,CAAC,OAAO,CAAyB,CAAC;AAC5D;;GAEG;AACH,KAAK,CAAC,YAAY,GAAG,IAAI,IAAI,CAAC,cAAc,CAAyB,CAAC;AACtE;;GAEG;AACH,KAAK,CAAC,OAAO,GAAG,IAAI,IAAI,CAAC,SAAS,CAAyB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/roles",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Roles package for unchained engine",
|
|
5
5
|
"main": "lib/roles-index.js",
|
|
6
6
|
"types": "lib/roles-index.d.ts",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"build": "tsc -b",
|
|
11
11
|
"prepublishOnly": "npm run clean && npm run build",
|
|
12
12
|
"watch": "tsc -w",
|
|
13
|
-
"test": "
|
|
14
|
-
"test:watch": "
|
|
13
|
+
"test": "tsx --test",
|
|
14
|
+
"test:watch": "tsx --test --watch"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
@@ -30,13 +30,8 @@
|
|
|
30
30
|
"url": "https://github.com/unchainedshop/unchained/issues"
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"lodash.clone": "4.5.0"
|
|
35
|
-
},
|
|
36
33
|
"devDependencies": {
|
|
37
|
-
"@types/lodash.clone": "^4.5.9",
|
|
38
34
|
"@types/node": "^22.13.4",
|
|
39
|
-
"jest": "^29.7.0",
|
|
40
35
|
"typescript": "^5.7.3"
|
|
41
36
|
}
|
|
42
37
|
}
|
package/src/roles.test.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { describe, it, beforeEach } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
1
3
|
import { isFunction, permissions, has } from './roles-index.js';
|
|
2
4
|
import { Role, Roles } from './roles.js';
|
|
3
5
|
|
|
@@ -7,14 +9,15 @@ describe('Role', () => {
|
|
|
7
9
|
Roles.actions = [];
|
|
8
10
|
Roles.helpers = [];
|
|
9
11
|
});
|
|
12
|
+
|
|
10
13
|
describe('constructor', () => {
|
|
11
14
|
it('should create a new role with the given name', () => {
|
|
12
|
-
|
|
15
|
+
assert.strictEqual(new Role('admin').name, 'admin');
|
|
13
16
|
});
|
|
14
17
|
|
|
15
18
|
it('should throw an error if a role with the same name already exists', () => {
|
|
16
19
|
new Role('admin');
|
|
17
|
-
|
|
20
|
+
assert.throws(() => new Role('admin'), /"admin" role is already defined/);
|
|
18
21
|
});
|
|
19
22
|
});
|
|
20
23
|
|
|
@@ -22,20 +25,20 @@ describe('Role', () => {
|
|
|
22
25
|
it('should add the helper function to the role', () => {
|
|
23
26
|
const role = new Role('admin');
|
|
24
27
|
role.helper('checkPermission', () => true);
|
|
25
|
-
|
|
28
|
+
assert.strictEqual(typeof role.helpers.checkPermission[0], 'function');
|
|
26
29
|
});
|
|
27
30
|
|
|
28
31
|
it('should add the helper to the list of helpers if it does not exist', () => {
|
|
29
32
|
const role = new Role('admin');
|
|
30
33
|
role.helper('checkPermission', () => true);
|
|
31
|
-
|
|
34
|
+
assert.deepStrictEqual(Roles.helpers, ['checkPermission']);
|
|
32
35
|
});
|
|
33
36
|
|
|
34
37
|
it('should convert a non-function value to a function that returns that value', () => {
|
|
35
38
|
const role = new Role('admin');
|
|
36
39
|
role.helper('checkPermission', true);
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
assert.strictEqual(typeof role.helpers.checkPermission[0], 'function');
|
|
41
|
+
assert.strictEqual(role.helpers.checkPermission[0](), true);
|
|
39
42
|
});
|
|
40
43
|
});
|
|
41
44
|
|
|
@@ -43,25 +46,25 @@ describe('Role', () => {
|
|
|
43
46
|
it('should add the allow function to the role', () => {
|
|
44
47
|
const role = new Role('admin');
|
|
45
48
|
role.allow('createUser', () => true);
|
|
46
|
-
|
|
49
|
+
assert.strictEqual(typeof role.allowRules.createUser[0], 'function');
|
|
47
50
|
});
|
|
48
51
|
|
|
49
52
|
it('should add the action to the list of actions if it does not exist', () => {
|
|
50
53
|
const role = new Role('admin');
|
|
51
54
|
role.allow('createUser', () => true);
|
|
52
|
-
|
|
55
|
+
assert.deepStrictEqual(Roles.actions, ['createUser']);
|
|
53
56
|
});
|
|
54
57
|
|
|
55
58
|
it('should throw an error if the action does not exist', () => {
|
|
56
59
|
const role = new Role('admin');
|
|
57
|
-
|
|
60
|
+
assert.throws(() => role.allow(null, () => true), /Action doesn't exist/);
|
|
58
61
|
});
|
|
59
62
|
|
|
60
63
|
it('should convert a non-function value to a function that returns that value', () => {
|
|
61
64
|
const role = new Role('admin');
|
|
62
65
|
role.allow('createUser', true);
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
assert.strictEqual(typeof role.allowRules.createUser[0], 'function');
|
|
67
|
+
assert.strictEqual(role.allowRules.createUser[0](), true);
|
|
65
68
|
});
|
|
66
69
|
});
|
|
67
70
|
|
|
@@ -72,95 +75,91 @@ describe('Role', () => {
|
|
|
72
75
|
it('should register an action rule', () => {
|
|
73
76
|
const allowFn = () => true;
|
|
74
77
|
testRole.allow(actionName, allowFn);
|
|
75
|
-
|
|
78
|
+
assert.strictEqual(typeof testRole.allowRules[actionName][0], 'function');
|
|
76
79
|
});
|
|
77
80
|
|
|
78
81
|
it('should register a helper', () => {
|
|
79
82
|
const helperFn = () => true;
|
|
80
83
|
testRole.helper('test_helper', helperFn);
|
|
81
|
-
|
|
84
|
+
assert.strictEqual(typeof testRole.helpers.test_helper[0], 'function');
|
|
82
85
|
});
|
|
83
86
|
});
|
|
87
|
+
|
|
84
88
|
describe('Role Helper Registration', () => {
|
|
85
89
|
it('should add a helper', () => {
|
|
86
90
|
Roles.registerHelper('test_helper');
|
|
87
|
-
|
|
91
|
+
assert.deepStrictEqual(Roles.helpers, ['test_helper']);
|
|
88
92
|
});
|
|
89
93
|
|
|
90
94
|
it('should add a helper attaching it to adminRole', () => {
|
|
91
95
|
Roles.registerHelper('test_admin_helper');
|
|
92
|
-
|
|
96
|
+
assert(Roles.helpers.includes('test_admin_helper'));
|
|
93
97
|
});
|
|
94
98
|
|
|
95
99
|
it('should skip adding helper if it already exists', () => {
|
|
96
100
|
Roles.registerHelper('test_helper');
|
|
97
101
|
Roles.registerHelper('test_admin_helper');
|
|
98
|
-
|
|
99
|
-
|
|
102
|
+
assert(Roles.helpers.includes('test_helper'));
|
|
103
|
+
assert(Roles.helpers.includes('test_admin_helper'));
|
|
100
104
|
});
|
|
101
105
|
});
|
|
102
106
|
|
|
103
107
|
describe('Action registration', () => {
|
|
104
108
|
it('should add an action', () => {
|
|
105
109
|
Roles.registerAction('test_action');
|
|
106
|
-
|
|
110
|
+
assert.deepStrictEqual(Roles.actions, ['test_action']);
|
|
107
111
|
});
|
|
108
112
|
|
|
109
113
|
it('should skip adding action if it already exists', () => {
|
|
110
114
|
Roles.registerAction('test_action');
|
|
111
|
-
|
|
115
|
+
assert.deepStrictEqual(Roles.actions, ['test_action']);
|
|
112
116
|
});
|
|
113
117
|
});
|
|
114
118
|
|
|
115
119
|
describe('Role contruction', () => {
|
|
116
120
|
it('should construct a new role', () => {
|
|
117
|
-
|
|
118
|
-
name: 'test_role',
|
|
119
|
-
allowRules: {},
|
|
120
|
-
helpers: {},
|
|
121
|
-
});
|
|
121
|
+
assert.equal(new Role('test_role') instanceof Role, true);
|
|
122
122
|
});
|
|
123
123
|
|
|
124
124
|
it('should throw an error if given a role with similar name', () => {
|
|
125
125
|
new Role('test_role');
|
|
126
|
-
|
|
126
|
+
assert.throws(() => new Role('test_role'));
|
|
127
127
|
});
|
|
128
128
|
});
|
|
129
129
|
|
|
130
130
|
describe('isFunction', () => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
isFunction(() => {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
).toBe(true);
|
|
131
|
+
it('should return true given a function', () => {
|
|
132
|
+
assert.strictEqual(
|
|
133
|
+
isFunction(() => ({})),
|
|
134
|
+
true,
|
|
135
|
+
);
|
|
137
136
|
});
|
|
138
137
|
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
it('should return false given an improper function', () => {
|
|
139
|
+
assert.strictEqual(isFunction('false' as any), false);
|
|
141
140
|
});
|
|
142
141
|
});
|
|
143
142
|
|
|
144
143
|
describe('has', () => {
|
|
145
|
-
|
|
144
|
+
it('should return true for existent key', () => {
|
|
146
145
|
const obj = {
|
|
147
146
|
foo: 'bar',
|
|
148
147
|
};
|
|
149
|
-
|
|
148
|
+
assert.strictEqual(has(obj, 'foo'), true);
|
|
150
149
|
});
|
|
151
150
|
|
|
152
|
-
|
|
151
|
+
it('should return true for existent nested key', () => {
|
|
153
152
|
const obj = {
|
|
154
153
|
foo: { bar: 'baz' },
|
|
155
154
|
};
|
|
156
|
-
|
|
155
|
+
assert.strictEqual(has(obj, 'foo.bar'), true);
|
|
157
156
|
});
|
|
158
157
|
|
|
159
|
-
|
|
158
|
+
it('should return false for non existent', () => {
|
|
160
159
|
const obj = {
|
|
161
160
|
foo: 'bar',
|
|
162
161
|
};
|
|
163
|
-
|
|
162
|
+
assert.strictEqual(has(obj, 'baz'), false);
|
|
164
163
|
});
|
|
165
164
|
});
|
|
166
165
|
});
|
|
@@ -177,7 +176,7 @@ describe('permissions', () => {
|
|
|
177
176
|
},
|
|
178
177
|
};
|
|
179
178
|
const result = await permissions(userRoles, allRoles);
|
|
180
|
-
|
|
179
|
+
assert.deepStrictEqual(result, []);
|
|
181
180
|
});
|
|
182
181
|
|
|
183
182
|
it('should return the correct permissions for a single user role', async () => {
|
|
@@ -191,7 +190,7 @@ describe('permissions', () => {
|
|
|
191
190
|
},
|
|
192
191
|
};
|
|
193
192
|
const result = await permissions(userRoles, allRoles);
|
|
194
|
-
|
|
193
|
+
assert.deepStrictEqual(result, ['createUser']);
|
|
195
194
|
});
|
|
196
195
|
|
|
197
196
|
it('should return the correct permissions for multiple user roles', async () => {
|
|
@@ -211,7 +210,7 @@ describe('permissions', () => {
|
|
|
211
210
|
},
|
|
212
211
|
};
|
|
213
212
|
const result = await permissions(userRoles, allRoles);
|
|
214
|
-
|
|
213
|
+
assert.deepStrictEqual(result, ['createUser', 'viewUser']);
|
|
215
214
|
});
|
|
216
215
|
|
|
217
216
|
it('should return unique and sorted permissions', async () => {
|
|
@@ -232,7 +231,7 @@ describe('permissions', () => {
|
|
|
232
231
|
},
|
|
233
232
|
};
|
|
234
233
|
const result = await permissions(userRoles, allRoles);
|
|
235
|
-
|
|
234
|
+
assert.deepStrictEqual(result, ['createUser', 'viewUser']);
|
|
236
235
|
});
|
|
237
236
|
});
|
|
238
237
|
|
|
@@ -240,14 +239,14 @@ describe('registerAction', () => {
|
|
|
240
239
|
it('should add a new action to the list of actions if it does not already exist', () => {
|
|
241
240
|
const action = 'createUser';
|
|
242
241
|
Roles.registerAction(action);
|
|
243
|
-
|
|
242
|
+
assert(Roles.actions.includes(action));
|
|
244
243
|
});
|
|
245
244
|
|
|
246
245
|
it('should not add the same action multiple times', () => {
|
|
247
246
|
const action = 'createUser';
|
|
248
247
|
Roles.registerAction(action);
|
|
249
248
|
Roles.registerAction(action);
|
|
250
|
-
|
|
249
|
+
assert.deepStrictEqual(Roles.actions, [action]);
|
|
251
250
|
});
|
|
252
251
|
});
|
|
253
252
|
|
|
@@ -255,14 +254,14 @@ describe('registerHelper', () => {
|
|
|
255
254
|
it('should add a new helper to the list of helpers if it does not already exist', () => {
|
|
256
255
|
const helper = 'checkAdmin';
|
|
257
256
|
Roles.registerHelper(helper);
|
|
258
|
-
|
|
257
|
+
assert(Roles.helpers.includes(helper));
|
|
259
258
|
});
|
|
260
259
|
|
|
261
260
|
it('should not add the same helper multiple times', () => {
|
|
262
261
|
const helper = 'checkAdmin';
|
|
263
262
|
Roles.registerHelper(helper);
|
|
264
263
|
Roles.registerHelper(helper);
|
|
265
|
-
|
|
264
|
+
assert.deepStrictEqual(Roles.helpers, [helper]);
|
|
266
265
|
});
|
|
267
266
|
});
|
|
268
267
|
|
|
@@ -273,7 +272,7 @@ describe('getUserRoles', () => {
|
|
|
273
272
|
const includeSpecial = true;
|
|
274
273
|
const expected = ['admin', '__all__', '__loggedIn__'];
|
|
275
274
|
const result = Roles.getUserRoles(userId, roles, includeSpecial);
|
|
276
|
-
|
|
275
|
+
assert.deepStrictEqual(result, expected);
|
|
277
276
|
});
|
|
278
277
|
|
|
279
278
|
it('should return the correct roles for a logged-out user', () => {
|
|
@@ -282,7 +281,7 @@ describe('getUserRoles', () => {
|
|
|
282
281
|
const includeSpecial = true;
|
|
283
282
|
const expected = ['admin', '__all__', '__notLoggedIn__'];
|
|
284
283
|
const result = Roles.getUserRoles(userId as any, roles, includeSpecial);
|
|
285
|
-
|
|
284
|
+
assert.deepStrictEqual(result, expected);
|
|
286
285
|
});
|
|
287
286
|
|
|
288
287
|
it('should return the correct roles when includeSpecial is false', () => {
|
|
@@ -291,6 +290,6 @@ describe('getUserRoles', () => {
|
|
|
291
290
|
const includeSpecial = false;
|
|
292
291
|
const expected = ['admin'];
|
|
293
292
|
const result = Roles.getUserRoles(userId, roles, includeSpecial);
|
|
294
|
-
|
|
293
|
+
assert.deepStrictEqual(result, expected);
|
|
295
294
|
});
|
|
296
295
|
});
|
package/src/roles.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import clone from 'lodash.clone';
|
|
2
1
|
import { has } from './utils/has.js';
|
|
3
2
|
import { isFunction } from './utils/isFunction.js';
|
|
4
3
|
|
|
@@ -164,7 +163,7 @@ export class Role implements RoleInterface {
|
|
|
164
163
|
let helperFn = func;
|
|
165
164
|
|
|
166
165
|
if (!isFunction(helperFn)) {
|
|
167
|
-
const clonedValue =
|
|
166
|
+
const clonedValue = structuredClone(helperFn);
|
|
168
167
|
helperFn = () => {
|
|
169
168
|
return clonedValue;
|
|
170
169
|
};
|
|
@@ -189,7 +188,7 @@ export class Role implements RoleInterface {
|
|
|
189
188
|
}
|
|
190
189
|
let allowFn = allow;
|
|
191
190
|
if (!isFunction(allowFn)) {
|
|
192
|
-
const clonedValue =
|
|
191
|
+
const clonedValue = structuredClone(allowFn);
|
|
193
192
|
allowFn = () => {
|
|
194
193
|
return clonedValue;
|
|
195
194
|
};
|
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
|
-
};
|