@sprucelabs/test-utils 5.4.1 → 5.4.3
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/README.md +0 -4
- package/build/decorators.d.ts +2 -2
- package/build/decorators.js +47 -31
- package/build/esm/decorators.d.ts +2 -2
- package/build/esm/decorators.js +45 -29
- package/package.json +1 -1
package/README.md
CHANGED
@@ -18,7 +18,3 @@ Spruce XP Documentation
|
|
18
18
|
<p align="center">
|
19
19
|
<a href="https://developer.spruce.ai/#/"><img width="250" src="https://raw.githubusercontent.com/sprucelabsai/spruce-test-utils/master/docs/images/read-full-docs.png" /></a>
|
20
20
|
</p>
|
21
|
-
|
22
|
-
### Dependencies
|
23
|
-
|
24
|
-
[Arkit diagram here](docs/dependencies.md).
|
package/build/decorators.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
export declare class
|
1
|
+
export declare class SpruceTestResolver {
|
2
2
|
static ActiveTestClass?: any;
|
3
3
|
private static __activeTest;
|
4
|
-
static
|
4
|
+
static resolveTestClass(target: any): any;
|
5
5
|
static getActiveTest(): any;
|
6
6
|
}
|
7
7
|
/** Test decorator */
|
package/build/decorators.js
CHANGED
@@ -1,50 +1,69 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.SpruceTestResolver = void 0;
|
4
4
|
exports.default = test;
|
5
5
|
exports.suite = suite;
|
6
6
|
if (typeof it === 'undefined') {
|
7
7
|
//@ts-ignore
|
8
8
|
global.it = () => { };
|
9
9
|
}
|
10
|
-
class
|
11
|
-
static
|
12
|
-
this.__activeTest
|
13
|
-
|
14
|
-
|
10
|
+
class SpruceTestResolver {
|
11
|
+
static resolveTestClass(target) {
|
12
|
+
if (!this.__activeTest) {
|
13
|
+
this.__activeTest = this.ActiveTestClass
|
14
|
+
? new this.ActiveTestClass()
|
15
|
+
: target;
|
16
|
+
}
|
15
17
|
return this.__activeTest;
|
16
18
|
}
|
17
19
|
static getActiveTest() {
|
18
20
|
return this.__activeTest;
|
19
21
|
}
|
20
22
|
}
|
21
|
-
exports.
|
23
|
+
exports.SpruceTestResolver = SpruceTestResolver;
|
24
|
+
//recursive function to get static method by name looping up through constructor chain
|
25
|
+
function resolveMethod(Target, name) {
|
26
|
+
if (Target[name]) {
|
27
|
+
return Target[name];
|
28
|
+
}
|
29
|
+
if (Target.constructor && Target.constructor !== Target) {
|
30
|
+
return resolveMethod(Target.constructor, name);
|
31
|
+
}
|
32
|
+
return null;
|
33
|
+
}
|
22
34
|
/** Hooks up before, after, etc. */
|
23
|
-
function
|
24
|
-
if (
|
35
|
+
function hookupTestClassToJestLifecycle(Target, h) {
|
36
|
+
if (Target.__isTestingHookedUp) {
|
25
37
|
return;
|
26
38
|
}
|
27
|
-
|
39
|
+
Target.__isTestingHookedUp = !h;
|
28
40
|
const hooks = h ?? ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'];
|
29
41
|
hooks.forEach((hook) => {
|
42
|
+
const cb = resolveMethod(Target, hook);
|
30
43
|
// Have they defined a hook
|
31
|
-
if (!
|
44
|
+
if (!cb) {
|
32
45
|
return;
|
33
46
|
}
|
34
|
-
if (SpruceTestDecoratorResolver.ActiveTestClass &&
|
35
|
-
!h &&
|
36
|
-
hook === 'beforeAll') {
|
37
|
-
throw new Error(`beforeAll() and afterAll() must be static`);
|
38
|
-
}
|
39
47
|
// @ts-ignore
|
40
48
|
if (global[hook]) {
|
41
49
|
// @ts-ignore
|
42
50
|
global[hook](async () => {
|
43
|
-
if (hook === '
|
44
|
-
|
45
|
-
|
51
|
+
if (hook === 'beforeEach') {
|
52
|
+
await SpruceTestResolver.resolveTestClass(Target).beforeEach();
|
53
|
+
}
|
54
|
+
else if (hook === 'afterEach') {
|
55
|
+
await SpruceTestResolver.resolveTestClass(Target).afterEach();
|
56
|
+
// @ts-ignore
|
57
|
+
delete SpruceTestResolver.__activeTest;
|
58
|
+
}
|
59
|
+
else {
|
60
|
+
if (SpruceTestResolver.ActiveTestClass) {
|
61
|
+
await cb.apply(Target.constructor);
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
await cb.apply(Target);
|
65
|
+
}
|
46
66
|
}
|
47
|
-
return target[hook]();
|
48
67
|
});
|
49
68
|
}
|
50
69
|
});
|
@@ -52,11 +71,10 @@ function hookupTestClass(target, h) {
|
|
52
71
|
/** Test decorator */
|
53
72
|
function test(description, ...args) {
|
54
73
|
return function (target, propertyKey, descriptor) {
|
55
|
-
|
56
|
-
hookupTestClass(target);
|
74
|
+
hookupTestClassToJestLifecycle(target);
|
57
75
|
// Make sure each test gets the spruce
|
58
76
|
it(description ?? propertyKey, async () => {
|
59
|
-
const testClass =
|
77
|
+
const testClass = SpruceTestResolver.resolveTestClass(target);
|
60
78
|
const bound = descriptor.value.bind(testClass);
|
61
79
|
//@ts-ignore
|
62
80
|
global.activeTest = {
|
@@ -69,19 +87,17 @@ function test(description, ...args) {
|
|
69
87
|
}
|
70
88
|
function suite() {
|
71
89
|
return function (Target) {
|
72
|
-
|
73
|
-
// Test.activeTest.__isTestingHookedUp = false
|
74
|
-
hookupTestClass(Target, ['beforeAll', 'afterAll']);
|
90
|
+
SpruceTestResolver.ActiveTestClass = Target;
|
75
91
|
};
|
76
92
|
}
|
77
93
|
/** Only decorator */
|
78
94
|
test.only = (description, ...args) => {
|
79
95
|
return function (target, propertyKey, descriptor) {
|
80
96
|
// Lets attach before/after
|
81
|
-
|
97
|
+
hookupTestClassToJestLifecycle(target);
|
82
98
|
// Make sure each test gets the spruce
|
83
99
|
it.only(description ?? propertyKey, async () => {
|
84
|
-
const bound = descriptor.value.bind(
|
100
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
85
101
|
return bound(...args);
|
86
102
|
});
|
87
103
|
};
|
@@ -90,7 +106,7 @@ test.only = (description, ...args) => {
|
|
90
106
|
test.todo = (description, ..._args) => {
|
91
107
|
return function (target, propertyKey) {
|
92
108
|
// Lets attach before/after
|
93
|
-
|
109
|
+
hookupTestClassToJestLifecycle(target);
|
94
110
|
// Make sure each test gets the spruce
|
95
111
|
it.todo(description ?? propertyKey);
|
96
112
|
};
|
@@ -99,10 +115,10 @@ test.todo = (description, ..._args) => {
|
|
99
115
|
test.skip = (description, ...args) => {
|
100
116
|
return function (target, propertyKey, descriptor) {
|
101
117
|
// Lets attach before/after
|
102
|
-
|
118
|
+
hookupTestClassToJestLifecycle(target);
|
103
119
|
// Make sure each test gets the spruce
|
104
120
|
it.skip(description ?? propertyKey, async () => {
|
105
|
-
const bound = descriptor.value.bind(
|
121
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
106
122
|
return bound(...args);
|
107
123
|
});
|
108
124
|
};
|
@@ -1,7 +1,7 @@
|
|
1
|
-
export declare class
|
1
|
+
export declare class SpruceTestResolver {
|
2
2
|
static ActiveTestClass?: any;
|
3
3
|
private static __activeTest;
|
4
|
-
static
|
4
|
+
static resolveTestClass(target: any): any;
|
5
5
|
static getActiveTest(): any;
|
6
6
|
}
|
7
7
|
/** Test decorator */
|
package/build/esm/decorators.js
CHANGED
@@ -2,43 +2,62 @@ if (typeof it === 'undefined') {
|
|
2
2
|
//@ts-ignore
|
3
3
|
global.it = () => { };
|
4
4
|
}
|
5
|
-
export class
|
6
|
-
static
|
7
|
-
this.__activeTest
|
8
|
-
|
9
|
-
|
5
|
+
export class SpruceTestResolver {
|
6
|
+
static resolveTestClass(target) {
|
7
|
+
if (!this.__activeTest) {
|
8
|
+
this.__activeTest = this.ActiveTestClass
|
9
|
+
? new this.ActiveTestClass()
|
10
|
+
: target;
|
11
|
+
}
|
10
12
|
return this.__activeTest;
|
11
13
|
}
|
12
14
|
static getActiveTest() {
|
13
15
|
return this.__activeTest;
|
14
16
|
}
|
15
17
|
}
|
18
|
+
//recursive function to get static method by name looping up through constructor chain
|
19
|
+
function resolveMethod(Target, name) {
|
20
|
+
if (Target[name]) {
|
21
|
+
return Target[name];
|
22
|
+
}
|
23
|
+
if (Target.constructor && Target.constructor !== Target) {
|
24
|
+
return resolveMethod(Target.constructor, name);
|
25
|
+
}
|
26
|
+
return null;
|
27
|
+
}
|
16
28
|
/** Hooks up before, after, etc. */
|
17
|
-
function
|
18
|
-
if (
|
29
|
+
function hookupTestClassToJestLifecycle(Target, h) {
|
30
|
+
if (Target.__isTestingHookedUp) {
|
19
31
|
return;
|
20
32
|
}
|
21
|
-
|
33
|
+
Target.__isTestingHookedUp = !h;
|
22
34
|
const hooks = h !== null && h !== void 0 ? h : ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'];
|
23
35
|
hooks.forEach((hook) => {
|
36
|
+
const cb = resolveMethod(Target, hook);
|
24
37
|
// Have they defined a hook
|
25
|
-
if (!
|
38
|
+
if (!cb) {
|
26
39
|
return;
|
27
40
|
}
|
28
|
-
if (SpruceTestDecoratorResolver.ActiveTestClass &&
|
29
|
-
!h &&
|
30
|
-
hook === 'beforeAll') {
|
31
|
-
throw new Error(`beforeAll() and afterAll() must be static`);
|
32
|
-
}
|
33
41
|
// @ts-ignore
|
34
42
|
if (global[hook]) {
|
35
43
|
// @ts-ignore
|
36
44
|
global[hook](async () => {
|
37
|
-
if (hook === '
|
38
|
-
|
39
|
-
|
45
|
+
if (hook === 'beforeEach') {
|
46
|
+
await SpruceTestResolver.resolveTestClass(Target).beforeEach();
|
47
|
+
}
|
48
|
+
else if (hook === 'afterEach') {
|
49
|
+
await SpruceTestResolver.resolveTestClass(Target).afterEach();
|
50
|
+
// @ts-ignore
|
51
|
+
delete SpruceTestResolver.__activeTest;
|
52
|
+
}
|
53
|
+
else {
|
54
|
+
if (SpruceTestResolver.ActiveTestClass) {
|
55
|
+
await cb.apply(Target.constructor);
|
56
|
+
}
|
57
|
+
else {
|
58
|
+
await cb.apply(Target);
|
59
|
+
}
|
40
60
|
}
|
41
|
-
return target[hook]();
|
42
61
|
});
|
43
62
|
}
|
44
63
|
});
|
@@ -46,11 +65,10 @@ function hookupTestClass(target, h) {
|
|
46
65
|
/** Test decorator */
|
47
66
|
export default function test(description, ...args) {
|
48
67
|
return function (target, propertyKey, descriptor) {
|
49
|
-
|
50
|
-
hookupTestClass(target);
|
68
|
+
hookupTestClassToJestLifecycle(target);
|
51
69
|
// Make sure each test gets the spruce
|
52
70
|
it(description !== null && description !== void 0 ? description : propertyKey, async () => {
|
53
|
-
const testClass =
|
71
|
+
const testClass = SpruceTestResolver.resolveTestClass(target);
|
54
72
|
const bound = descriptor.value.bind(testClass);
|
55
73
|
//@ts-ignore
|
56
74
|
global.activeTest = {
|
@@ -63,19 +81,17 @@ export default function test(description, ...args) {
|
|
63
81
|
}
|
64
82
|
export function suite() {
|
65
83
|
return function (Target) {
|
66
|
-
|
67
|
-
// Test.activeTest.__isTestingHookedUp = false
|
68
|
-
hookupTestClass(Target, ['beforeAll', 'afterAll']);
|
84
|
+
SpruceTestResolver.ActiveTestClass = Target;
|
69
85
|
};
|
70
86
|
}
|
71
87
|
/** Only decorator */
|
72
88
|
test.only = (description, ...args) => {
|
73
89
|
return function (target, propertyKey, descriptor) {
|
74
90
|
// Lets attach before/after
|
75
|
-
|
91
|
+
hookupTestClassToJestLifecycle(target);
|
76
92
|
// Make sure each test gets the spruce
|
77
93
|
it.only(description !== null && description !== void 0 ? description : propertyKey, async () => {
|
78
|
-
const bound = descriptor.value.bind(
|
94
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
79
95
|
return bound(...args);
|
80
96
|
});
|
81
97
|
};
|
@@ -84,7 +100,7 @@ test.only = (description, ...args) => {
|
|
84
100
|
test.todo = (description, ..._args) => {
|
85
101
|
return function (target, propertyKey) {
|
86
102
|
// Lets attach before/after
|
87
|
-
|
103
|
+
hookupTestClassToJestLifecycle(target);
|
88
104
|
// Make sure each test gets the spruce
|
89
105
|
it.todo(description !== null && description !== void 0 ? description : propertyKey);
|
90
106
|
};
|
@@ -93,10 +109,10 @@ test.todo = (description, ..._args) => {
|
|
93
109
|
test.skip = (description, ...args) => {
|
94
110
|
return function (target, propertyKey, descriptor) {
|
95
111
|
// Lets attach before/after
|
96
|
-
|
112
|
+
hookupTestClassToJestLifecycle(target);
|
97
113
|
// Make sure each test gets the spruce
|
98
114
|
it.skip(description !== null && description !== void 0 ? description : propertyKey, async () => {
|
99
|
-
const bound = descriptor.value.bind(
|
115
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
100
116
|
return bound(...args);
|
101
117
|
});
|
102
118
|
};
|